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

公司建网站费用关键词歌曲免费听

公司建网站费用,关键词歌曲免费听,设计说明室内设计现代简约,推广网上国网的好处148. 排序链表 题目:给你链表的头结点 head ,请将其按升序排列并返回排序后的链表。 题目链接: 148. 排序链表 时间复杂度:快排 O(n^2) 超出时间限制 class Solution {public ListNode sortList(ListNode head) {if(headnull)…

148. 排序链表

题目:给你链表的头结点 head ,请将其按升序排列并返回排序后的链表。
在这里插入图片描述

题目链接: 148. 排序链表
时间复杂度:快排 O(n^2) 超出时间限制

class Solution {public ListNode sortList(ListNode head) {if(head==null){return head;}ListNode dummy=new ListNode(Integer.MIN_VALUE,null);ListNode pointnew=dummy;ListNode pointold=head;while(pointold!=null){while(pointnew!=null&&pointnew.next!=null){if(pointold.val<=pointnew.next.val){ListNode next=pointnew.next;ListNode node=new ListNode(pointold.val);pointnew.next=node;node.next=next;pointnew=dummy;break;}else{pointnew=pointnew.next;}}if(pointnew.next==null){ListNode next=pointnew.next;ListNode node=new ListNode(pointold.val);pointnew.next=node;node.next=next;pointnew=dummy;}pointold=pointold.next;}return dummy.next;}
}

归并排序O(logn):

class Solution {public ListNode sortList(ListNode head) {if(head==null||head.next==null){return head;}//找中点截断链表ListNode fast = head;ListNode slow = head;ListNode pre=null;while(fast!=null&&fast.next!=null){pre=slow;slow=slow.next;fast=fast.next.next;}//递归截断链表pre.next=null;ListNode left=sortList(head);ListNode right=sortList(slow);//合并链表ListNode dummy=new ListNode(0);ListNode res = dummy;while (left != null && right != null) {if (left.val < right.val) {res.next = left;left = left.next;} else {res.next = right;right = right.next;}res=res.next;}res.next=left!=null?left:right;return dummy.next;}
}

归并排序迭代方法 时间复杂度O(logn),空间复杂度为O(1):
直接当作n个长度为1的链表进行归并 先归并为2个有序,继而4,8…直到其长度大于链表长度n

public ListNode sortList(ListNode head) {if (head == null || head.next == null) {return head;}// 获取链表长度int length = 0;ListNode current = head;while (current != null) {length++;current = current.next;}ListNode dummy = new ListNode(0);dummy.next = head;ListNode left, right, tail;// 每次翻倍增加子链表的长度for (int step = 1; step < length; step *= 2) {current = dummy.next;tail = dummy;while (current != null) {left = current;right = split(left, step); // 分割出两个子链表current = split(right, step); //划分下一个lefttail = merge(left, right, tail); // 合并两个子链表}}return dummy.next;}// 分割链表private ListNode split(ListNode head, int step) {if (head == null) return null;for (int i = 1; head.next != null && i < step; i++) {head = head.next;}ListNode right = head.next;head.next = null;return right;}// 合并两个链表private ListNode merge(ListNode l1, ListNode l2, ListNode tail) {ListNode current = tail;while (l1 != null && l2 != null) {if (l1.val < l2.val) {current.next = l1;l1 = l1.next;} else {current.next = l2;l2 = l2.next;}current = current.next;}current.next = (l1 != null) ? l1 : l2;while (current.next != null) {current = current.next;}return current;}
http://www.dt0577.cn/news/13324.html

相关文章:

  • 网站界面设计如何实现功能美与形式美的统一?seo网站设计
  • 浙江新华建设有限公司网站sem账户托管
  • 青海政府网站建设公司短链接购买
  • 东莞网站建设seo优化网站自动推广软件免费
  • 阿里云企业网站建设教程营销型网站制作企业
  • 当阳建设中学网站软件开发app制作公司
  • 网站备案备案吗昆明seo案例
  • 网站制作的部分seo网络推广公司排名
  • 动物园网站建设的可行性分析网站关键词查询网址
  • 南昌网站建设价格360网站推广怎么做
  • 青岛手机网站制作宁波seo怎么做优化
  • 桂林市防疫最新政策搜索引擎优化的主要手段
  • 黑龙江建设厅网站账户竞价托管费用
  • 佛山网站建设品牌网站收录入口申请查询
  • 网站市场推广500强企业seo服务商
  • 美国外贸网站建设镇江百度推广公司
  • 品牌网站官网seo简单速排名软件
  • 哈尔滨百度seo公司抖音搜索seo代理
  • 北京活动网站制作自己如何建立网站
  • 百色做网站百度热搜榜排行
  • 企业官方网站如何做蓝v认证武汉网络推广自然排名
  • 网站建设的价值是什么网络推广服务外包
  • 老域名重新做网站seo关键词排名优化报价
  • 国外销售网站怎样建设怎么让百度搜出自己
  • 京东网站建设过程百度竞价最低点击一次多少钱
  • 顺德网站设计成人用品推广网页
  • 打码网站怎么做品牌宣传文案范文
  • 门户网站源码入驻app开发费用
  • 做色情网站需要多少钱广告图片
  • 排版设计说明系统优化大师