当前位置: 首页 > wzjs >正文

html网页素材野狼seo团队

html网页素材,野狼seo团队,wordpress主题开发出,免费的网站管理系统解法一:(双重循环)第一个循环head,逐步将head的node加入有序列表;第二个循环在有序列表中找到合适的位置,插入node。 /*** Definition for singly-linked list.* public class ListNode {* int val;* …

解法一:(双重循环)第一个循环head,逐步将headnode加入有序列表;第二个循环在有序列表中找到合适的位置,插入node

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode sortList(ListNode head) {// 建立一个头节点ListNode newHead = new ListNode();// 建立辅助指针ListNode pre, curr, temp;// 把第一个node放进去if(head==null){return null;}temp = head.next;newHead.next = head;head.next = null;head = temp;// 循环while(head != null){pre = newHead;curr = newHead.next;while(curr.val<head.val){pre = pre.next;curr = curr.next;if(curr==null){break;}}temp = head.next;head.next = curr;pre.next = head;head = temp;}return newHead.next;}
}

注意:

  • 要在第二个循环内判断curr==null,不能在第二个循环while()中判断。即curr==null的判断要先于curr.val<head.val判断。
  • 在使用head.next之前,要先判断head==null

错误原因:

在这里插入图片描述

解法二:自顶向下归并排序

对链表自顶向下归并排序的过程如下:

  • 找到链表的中点,以中点为分界,将链表拆分成两个子链表。寻找链表的中点可以使用快慢指针的做法,快指针每次移动 2 步,慢指针每次移动 1步,当快指针到达链表末尾时,慢指针指向的链表节点即为链表的中点。
  • 对两个子链表分别排序。
  • 将两个排序后的子链表合并,得到完整的排序后的链表。可以使用「21. 合并两个有序链表」的做法,将两个有序的子链表进行合并。
/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode sortList(ListNode head) {return sortList(head, null);}public ListNode sortList(ListNode head, ListNode tail){// 中止条件if(head==null){return null;}if(head.next==tail){head.next=null;return head;}// 设置快慢指针找中点ListNode faster=head, slower=head;while(faster!=tail){faster=faster.next;slower=slower.next;if(faster!=tail){faster=faster.next;}}ListNode mid=slower;ListNode list1=sortList(head,mid);ListNode list2=sortList(mid,tail);// 排序两个有序序列return merge(list1,list2);}public ListNode merge(ListNode list1, ListNode list2) {ListNode new_list=null;if(list1 != null && list2 != null){if(list1.val<=list2.val){new_list=list1;list1=list1.next;new_list.next=null;}else{new_list=list2;list2=list2.next;new_list.next=null;}}else{return list1!=null?list1:list2;}ListNode curr=new_list;while(list1 != null && list2 != null){if(list1.val<=list2.val){ListNode temp = list1.next;curr.next=list1;list1.next=null;list1=temp;}else{ListNode temp = list2.next;curr.next=list2;list2.next=null;list2=temp;}curr=curr.next;}if(list1!=null){curr.next=list1;}if(list2!=null){curr.next=list2;}return new_list;}
}

注意:

  • 中止条件:head==nullhead.next==tail
  • faster走第二步的时候也要判断faster!=tail
http://www.dtcms.com/wzjs/51338.html

相关文章:

  • 免费网站图片素材合肥今天的最新消息
  • 网络营销策划书1000字seowhy教研室
  • 外国网站邀请做编辑产品推广活动策划方案
  • 专业做网站哪里好网站优化seo
  • 网站服务包括什么备案查询
  • 66公里智慧门店管理系统咸宁网站seo
  • 手机网站前端写法seo指导
  • 常州小程序开发报价湖南企业竞价优化公司
  • 做网站常规语言查权重网站
  • 网站做淘宝客赚钱吗手机管家一键优化
  • 网站前期运营策略网络舆情监测与研判
  • 网站的网站建设公司成都seo推广员
  • 西安免费网站建设石家庄新闻网
  • 杭州做网站一般多少钱第一营销网
  • 重庆城乡建设委员会网站百度指数官网
  • 温州电子商务网站建设seo搜索引擎入门教程
  • 北京的公司排名seo快速排名软件推荐
  • 福州网站设计招聘优化搜狗排名
  • 做网站是买服务器还是买cdn最火的网络推广平台
  • 深圳seo推广公司福州关键词排名优化
  • cn后缀做网站网络平台的推广方法
  • 龙岩做网站开发找哪家安徽网站seo
  • 开发网站找什么公司友情链接交换要注意哪些问题
  • win10记事本怎么做网站长沙网络推广公司
  • 360网站seo怎么做app定制开发
  • 有哪些专门做校企合作的网站做关键词排名好的公司
  • 聊城 网站制作怎样查询百度收录和排名情况
  • 动漫网站建设总结什么是关键词排名优化
  • 上海网站开发与设计微信营销软件排行榜
  • 解决wordpress慢seo快速排名软件