Sort a linked list in O(n log n) time using constant space complexity.

记得Insert Sort List, 那个复杂度是O(N^2)的,这里要求O(nlogn),所以想到merge sort, 需要用到Merge Two Sorted List的方法(我写的merge函数)

第二遍代码:所以mergesort不好写在于它是一个递归里嵌套另一个递归,第一个递归不停地用runner technique把list分两段,直到每一段是一个或0个节点返回该点,这时再调用merge Two sorted List递归把两段整合起来,返回它们的首节点

 1 public class Solution {
 2     public ListNode sortList(ListNode head) {
 3         if (head == null || head.next == null) return head;
 4         return mergesort(head);
 5     }
 6
 7     public ListNode mergesort(ListNode head) {
 8         if (head == null || head.next == null) return head;
 9         ListNode dummy = new ListNode(-1);
10         dummy.next = head;
11         ListNode walker = dummy;
12         ListNode runner = dummy;
13         while (runner!=null && runner.next!=null) {
14             runner = runner.next.next;
15             walker = walker.next;
16         }
17         ListNode head1 = dummy.next;
18         ListNode head2 = walker.next;
19         walker.next = null;
20         return merge(mergesort(head1), mergesort(head2));
21     }
22
23     public ListNode merge(ListNode head1, ListNode head2) {
24         if (head1 == null) return head2;
25         if (head2 == null) return head1;
26         ListNode dummy = new ListNode(-1);
27         dummy.next = head1;
28         ListNode pre = dummy;
29         while (head1 != null && head2 != null) {
30             if (head1.val <= head2.val) {
31                 head1 = head1.next;
32             }
33             else {
34                 ListNode next = head2.next;
35                 head2.next = pre.next;
36                 pre.next = head2;
37                 head2 = next;
38             }
39             pre = pre.next;
40         }
41         if (head2 != null) {
42             pre.next = head2;
43         }
44         return dummy.next;
45     }
46 }

转载于:https://www.cnblogs.com/EdwardLiu/p/3978383.html

Leetcode: Sort List相关推荐

  1. LeetCode: Sort List 解题报告

    Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...

  2. LeetCode Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 思路分析:这题要求在O(n log n) 和常量空间对单链 ...

  3. [leetcode]Sort Colors

    问题描写叙述: Given an array with n objects colored red, white or blue, sort them so that objects of the s ...

  4. Leetcode | Sort List

    Sort a linked list in O(n log n) time using constant space complexity. merge sort.heap sort和quick so ...

  5. leetcode:Sort List(一个链表的归并排序)

    Sort a linked list in O(n log n) time using constant space complexity. 分析:题目要求时间复杂度为O(nlogn),所以不能用qu ...

  6. LeetCode Sort Colors

    1.题目 Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  7. LeetCode -- Sort List

    Question: Sort a linked list in O(n log n) time using constant space complexity. Analysis: 问题描述:在O(n ...

  8. LeetCode Sort Characters By Frequency

    题意:给出一个字符串s,按字符出现频率排序 代码如下: class Solution(object):def frequencySort(self, s):""":typ ...

  9. LeetCode Sort List(单链表归并排序)

    题意:给出一个单链表,将其排序,要求时间复杂度O(nlgn) 思路:用归并排序,取链表的一半,在取一半时,不用先计算总结点个数,一个每次走两步,一个每次走一步,当走的快的结束是,慢的已经到总结点的一半 ...

最新文章

  1. ros udp协议53端口 封_每天一点IT-说说AD域的端口
  2. android studio 抓log,Android studio保存logcat日志到本地的操作
  3. 重磅风控干货:如何用数据分析监测交易欺诈
  4. log4j 2.x 架构
  5. perform build_lc_user_stat
  6. xd可以用ui动效效果吗_通过动画使UI设计栩栩如生:Adobe XD和After Effects
  7. LeetCode 22. 括号生成(回溯/DP)
  8. sql server表分区_SQL Server中的FORCESCAN和分区表
  9. JQuery获取当前屏幕的高度宽度
  10. rtmp之谷歌浏览器默认启用flash
  11. 智能手机屏幕尺寸和分辨率一览表
  12. Docker Windows Containers
  13. 《小鑫发现》之GraphQL框架Prisma
  14. 前端实现内网在线excel编辑和word在线编辑相关建议
  15. python学什么内容_老男孩Python都需要学什么内容?老男孩教育
  16. 估值调整 - 时间调整
  17. TVS 管选型与 ESD 防护设计
  18. 【对称加密与非对称加密】-- 概念、图示、优缺点
  19. 在外企必会的英文单词
  20. 利用傅里叶变换去除图像中有规律的噪声

热门文章

  1. 浅析Hyperledger Fabric共识算法
  2. Git合并和变基简介:它们是什么,以及如何使用它们
  3. javascript_治愈JavaScript疲劳的研究计划
  4. classlist使用方法_如何通过使用HTML5的classList API在没有jQuery的情况下操作类
  5. PAT(甲级)2020年春季考试 7-4 Replacement Selection
  6. 初学者如何学Java开发
  7. java 获取init参数,Java如何读取servlet init参数?
  8. 1-1 分配内存资源给容器和POD
  9. Python 爬虫练手项目—酒店信息爬取
  10. 使用Mono管理Coyote Linux