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

石柱网站制作石家庄建站软件

石柱网站制作,石家庄建站软件,上海建筑企业资质查询平台,WordPress怎么找主题方法1:插入方法进行改进 class Solution {public ListNode sortList(ListNode head) {/*想法#xff1a;设置两个指针first,last分别指向当前有序子链表的头和尾节点#xff1b;并遍历链表#xff0c;当遍历到的节点值大于last的值时#xff0c;就将该节点插入到有序子链表…方法1:插入方法进行改进 class Solution {public ListNode sortList(ListNode head) {/*想法设置两个指针first,last分别指向当前有序子链表的头和尾节点并遍历链表当遍历到的节点值大于last的值时就将该节点插入到有序子链表表尾值小于first时插入到子链表表头处于二者中间时就遍历进行插入*/if(head null)return null;ListNode first head,last head;ListNode p head.next;head.next null;while(p ! null){ListNode temp p.next;if(p.val last.val){//插入表尾last.next p;p.next null;last p;}else if(p.val first.val){//插入表头p.next first;first p;}else{// 遍历进行插入for(ListNode q first;q ! last ;q q.next){if(q.next.val p.val){p.next q.next;q.next p;break;}}} p temp;}return first;} } 方法2:自顶向下的归并排序 归并排序的时间复杂度问题在每一层中进行寻找中间节点有序链表进行两两合并都需要2n,而归并排序总共会进行logn层处理因此最终的时间复杂度就是O(nlogn)。 class Solution {public ListNode sortList(ListNode head) {/*自顶向下的归并排序首先寻找中间节点在利用归并排序将当前需要排序的链表进行两两分别排序最后在通过合并两个有序链表的方式以及递归的方式进行排序。*/if(head null)return null;return sortSubList(head,null);}//将链表进行排序(tail指向)public ListNode sortSubList(ListNode head,ListNode tail){if(head.next null)return head;if(head.next tail){head.next null;return merge(head,tail);}//先找到链表的中间节点ListNode slow head,fast head.next.next;while(fast ! tail){slow slow.next;fast fast.next;if(fast ! tail)fast fast.next;}//将左边的子链表表尾指向空指针右边子链表表尾本就是空指针ListNode subHead2 slow.next;slow.next null;ListNode head1 sortSubList(head,slow);ListNode head2 sortSubList(subHead2,tail);return merge(head1,head2);}//将两个子链表进行排序并合并返回合并后的链表头节点//判断是否到了尾,即是否到了空节点即可public ListNode merge(ListNode head1,ListNode head2){ListNode head new ListNode(-1,null); ListNode temp head,temp1 head1,temp2 head2;while(temp1 ! null temp2 ! null){if(temp1.val temp2.val){temp.next temp1;temp1 temp1.next;}else{temp.next temp2;temp2 temp2.next;}temp temp.next;}while(temp1 ! null){temp.next temp1;temp1 temp1.next;temp temp.next;}while(temp2 ! null){temp.next temp2;temp2 temp2.next;temp temp.next;}return head.next;} } 方法3自底向上的归并排序 class Solution {public ListNode sortList(ListNode head) {/*自顶向下的归并排序首先寻找中间节点在利用归并排序将当前需要排序的链表进行两两分别排序最后在通过合并两个有序链表的方式以及递归的方式进行排序。*/if(head null)return null;//遍历链表获取链表长度int length 0;ListNode p head;while(p ! null){length;p p.next;}ListNode hair new ListNode(-1,head);for(int subLength 1;subLength length;subLength * 2){//开始遍历链表获取子链表并两两合并ListNode pre hair, cur hair.next;while(cur ! null){//获取一个的子链表ListNode head1 cur;for(int i 1;i subLength cur.next !null;i){cur cur.next;}ListNode head2 cur.next;cur.next null;cur head2;//再获取一个子链表for(int i 1;i subLength cur!null;i){cur cur.next;}if(cur ! null){ListNode temp cur.next;cur.next null;cur temp;}ListNode mergeResult merge(head1,head2);//pre指针指向当前两个子链表的前面的一个节点pre.next mergeResult;while(pre.next ! null)pre pre.next;}}return hair.next;}//将两个子链表进行排序并合并返回合并后的链表头节点//判断是否到了尾,即是否到了空节点即可public ListNode merge(ListNode head1,ListNode head2){ListNode head new ListNode(-1,null); ListNode temp head,temp1 head1,temp2 head2;while(temp1 ! null temp2 ! null){if(temp1.val temp2.val){temp.next temp1;temp1 temp1.next;}else{temp.next temp2;temp2 temp2.next;}temp temp.next;}while(temp1 ! null){temp.next temp1;temp1 temp1.next;temp temp.next;}while(temp2 ! null){temp.next temp2;temp2 temp2.next;temp temp.next;}return head.next;} }
http://www.hkea.cn/news/14343503/

相关文章:

  • 如何自己建立网站建设海南网络广播电视台地震避险常识
  • 网站价格淘宝联盟怎么做自已的网站
  • 请给自己的网站首页布局怎么做自己的html网站
  • 做网站 哪些公司网站优化人员
  • 企业建站有什么好处wordpress 单页模版
  • php做用户登录网站为什么网站建设需要每年续费
  • 动易网站频道栏目字体大小修改地方同城网站开发
  • 咋样查看网站用什么编程语言做的南充二手房最新出售信息
  • 网页制作企业网站作业官方网站建设教程
  • 网站怎样改logoopenshift安装wordpress密码忘记
  • node.js 做网站宜章网站建设
  • 上海企业建站公司排名广东建设项目备案公示网站
  • 网站建设有限公怎么做招聘有哪些网站
  • 做网站时需要FTP工具吗专业自适应网站建设极速建站
  • 定西市小企业网站建设建设物流网站的分类
  • 网页设计与网站建设考试名词解释手机营销网站建设
  • 重庆装修网站建设高端企业展厅设计公司
  • 邢台网站建设策划领券购买网站是怎么做的
  • 青岛建设集团官方网站车行网站源码
  • 做网站的公司 设计好尚石设计深圳有限公司
  • 下载男女做爰免费网站和田地区建设局网站
  • 上海外贸网站推广[wordpress
  • 做网站需要哪些费用支出电商设计师招聘
  • 做淘宝客网站需要什么做植物提取物的专业网站
  • 做网站首页的图片怎么缩小台州专业制作网站
  • 网站内容优化方法有哪些十堰专业网站设计制作
  • 免费行情软件网站游戏wordpress支付宝支付
  • 支付宝小程序搜索引擎禁止的方式优化网站
  • 医院网站建设技术方案ppt专业网站设计 软件
  • 国开网站怎么做网站建设主题大全