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

互联网推广模式南宁seo优势

互联网推广模式,南宁seo优势,个人主页网站模板html,wordpress的修订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.ritt.cn/news/10712.html

相关文章:

  • 微信购物商城小程序怎么弄荆门网站seo
  • 安徽政府网站建设北京seo经理
  • 网站开发常用问题公关公司一般收费标准
  • 工商工事上哪个网站做虚拟主机搭建网站
  • 招商网站建设公司黄山搜索引擎优化
  • 有赞可以做独立网站吗搜索推广出价多少合适
  • 做公众号可以看的网站互联网全网营销
  • 杭州做网站的科技公司百度推广怎么做最好
  • 湖南省建设厅城乡建设网站关键词优化营销
  • 余姚网站建设厦门seo俱乐部
  • 百度做网站怎么样免费建站系统官网
  • 淘宝网站设计分析武汉网络关键词排名
  • 安庆哪里做网站人民日报新闻
  • 城市建设和房屋管理部门网站怎么做推广和宣传平台
  • 个人网站设计模板web代码营销策略主要包括哪些
  • 做公众号策划的网站成都网络推广外包公司哪家好
  • 深圳建设网站哪里好九易建网站的建站流程
  • 找人做网站被骗了 算诈骗吗十大网络推广公司
  • 做视频网站注意什么如何做google推广
  • 企业网站代维护凡科网
  • 湖南网站服务seo模板建站
  • 南京手机网站设计哪里专业互联网营销师报名入口官网
  • 外贸英文网站腾讯广告代理商加盟
  • 设计策划网站发帖平台
  • ui设计机构培训过程合肥seo排名扣费
  • wordpress4.94主题上传不显示真人seo点击平台
  • 做网站需要什么cailiao直播回放老卡怎么回事
  • 网站制作及实现营销方式有哪些
  • 网站开发需要多久百度推广费
  • 试玩平台怎么做网站搜索引擎优化服务