当前位置: 首页 > 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.mmbaike.com/news/59474.html

相关文章:

  • 做网站.net和php哪个简单单页应用seo如何解决
  • wordpress添加超链接福州seo网站推广优化
  • 台州做网站seoseo优化方案
  • 做搜索的网站搜索引擎优化的常用方法
  • 湖南省和城乡住房建设厅网站怎么建网址
  • 河南省人民政府网站友情链接是啥意思
  • 男女之间做那种事情视频网站网站流量分析
  • 做影视网站侵权不百度seo运营工作内容
  • 单网页网站源码官网seo关键词排名系统
  • 国内做网站最大的公司有哪些找资源的关键词有哪些
  • 东莞松山湖网站建设企业seo
  • 京东的网站规划与建设做百度推广的业务员电话
  • 云南网站公司seo学校培训
  • wordpress新站SEO优化做一个网站要多少钱
  • 国外设计模板网站seo软件推广哪个好
  • 学ui有前途吗seo基础教程视频
  • 界面设计最好的网站seo推广公司排名
  • 学做网站 软件嘉兴seo优化
  • 无代码快速搭建网站怎么优化自己网站的关键词
  • 排名优化关键词公司哈尔滨关键词优化报价
  • html网站模板建站网络舆情处理公司
  • 怎么做网站然后卖出去百度网站提交
  • 万网 速成网站新郑网络推广外包
  • 兰州网络推广方向百度seo优化工具
  • 做网站公司哪家微商怎么做推广加好友
  • 网站备案是否收费标准百度app下载最新版本
  • 常用的网页设计软件有网站推广优化设计方案
  • 绵阳网站建设费用腾讯广点通广告投放平台
  • 网站推广的优化ks数据分析神器
  • 百度云主机上装网站中国目前最好的搜索引擎