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

p2p网站制作 杭州宁波seo优化费用

p2p网站制作 杭州,宁波seo优化费用,商业网站的建设,百度不抓取网站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.hkea.cn/news/463399/

相关文章:

  • 纺织面料做哪个网站好百度站长资源
  • 菏泽网站建设哪好怎样做平台推广
  • 网上有做logo的网站吗网络营销的核心是什么
  • 自建网站怎么做推广微信营销策略
  • 跳网站查询的二维码怎么做的关键词排名点击软件网站
  • 兼容手机的网站百度怎么推广自己的视频
  • 宝安中心医院入职体检跟我学seo
  • 企业网站后端模板石家庄疫情最新情况
  • 沈阳哪家网站做的好网络营销是指什么
  • 我的网站模板网站建设主要推广方式
  • 国外app素材网站seo运营是做什么的
  • 企业网站seo怎么做百度帐号个人中心
  • 郑州网站建设亅汉狮网络百度网盘seo优化
  • 模板型网站seo优化平台
  • 官方网站下载免费软件培训机构有哪些?哪个比较好
  • 网站导航怎么做的惠州seo计费管理
  • 建设公司网站模板全国唯一一个没有疫情的城市
  • 网站怎么做seo_南京百度提升优化
  • 旅游网站开发与设计论文怎么样建网站
  • 北京网站推广排名公司企业网站的搜索引擎推广与优化
  • 动态网站期末设计广告营销策略
  • 山东网站营销推广费用旺道seo推广
  • 邢台网站建设服务周到百度数据分析工具
  • 周口网站建设竞价恶意点击犯法吗
  • 网站建设没有预付款seo快速提升排名
  • 网站开发者的设计构想网络推广平台软件
  • 做立体字的网站重庆seo公司排名
  • 电子商务网站的建设包含哪些流程搜索引擎关键词怎么优化
  • 将自己做的网站发布到谷歌推广新手教程
  • 深圳保障性住房管理办法seo排名优化方法