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

merge sort、heap sort和quick sort都是O(nlgn),但是mergesort和quicksort都是递归的,不是constant space的,heapsort需要有随机访问才行。

mergesort的bottom up实现是可以常数空间的。也适用于链表。

如果是数组,还可以用heapsort。

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     // merge two list, and return last element (not NULL)
12     ListNode* merge(ListNode *nh, ListNode *p1, ListNode *p2) {
13         while (p1 || p2) {
14             if (p1 && (p2 == NULL || p1->val < p2->val)) {
15                 nh->next = p1;
16                 p1 = p1->next;
17             } else {
18                 nh->next = p2;
19                 p2 = p2->next;
20             }
21             nh = nh->next;
22         }
23         return nh;
24     }
25
26     // get length of list
27     int getLength(ListNode *head) {
28         int n = 0;
29         while (head) {
30             n++;
31             head = head->next;
32         }
33         return n;
34     }
35
36     ListNode *sortList(ListNode *head) {
37         int n = getLength(head);
38         ListNode *p1, *p2, *tmp, *newH1, *tail;
39
40         // merge sort, bottom up
41         for (int l = 1; l < n; l *= 2) {
42             p1 = head;
43             tail = head = NULL; // head of the whole list
44             while (p1) {
45                 p2 = p1;
46                 for (int i = 1; i < l && p2; ++i) {
47                     p2 = p2->next;
48                 }
49                 if (!p2) break;
50                 tmp = p2->next;
51                 p2->next = NULL; // set tail of list 1 to NULL
52                 p2 = tmp;
53                 for (int i = 1; i < l && tmp; ++i) {
54                     tmp = tmp->next;
55                 }
56                 if (tmp) {
57                     newH1 = tmp->next; // get next head of list 1
58                     tmp->next = NULL; // set tail of list 2 to NULL
59                 } else {
60                     newH1 = NULL;
61                 }
62                 ListNode h(0);
63                 ListNode *last = merge(&h, p1, p2);
64                 if (tail) tail->next = h.next; // connect the sorted part with the current two list
65                 if (!head) head = h.next;
66                 tail = last;
67                 last->next = newH1;
68                 p1 = newH1;
69             }
70         }
71         return head;
72     }
73 };

转载于:https://www.cnblogs.com/linyx/p/3824628.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 List

    Sort a linked list in O(n log n) time using constant space complexity. 记得Insert Sort List, 那个复杂度是O(N ...

  4. [leetcode]Sort Colors

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

  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. 深刻揭示Python 与C/C++互动
  2. 企业命令linux下处理实时日志生成另一个实时日志
  3. QJson生成文件和解析文件
  4. stm32f103c8t6掉坑记
  5. oracle数据本机自动备份
  6. 人事面试的那些问题及背后的考察点
  7. panel,dialog,window组件越界问题汇总
  8. pyqt与mysql例子_PyQt 连接MySql数据库,C++代码转Python3代码
  9. 使用jQuery获取GridView的数据行的数量
  10. python字典类型可迭代_核心数据类型--字典
  11. 设计模式学习每天一个——Decorator模式
  12. [py]flask从0到1-模板/增删改查
  13. Jenkins控制台出现中文乱码
  14. python检查exe运行是否报错_python打包成exe格式后,在部分机子上没法运行
  15. U盘装完系统后,不能用,写保护,无法格式化,变为RAW解决办法
  16. coreldraw怎样定数等分_CorelDRAW基础教程,教你cdr如何等分分割图片
  17. 如何从0到开始写短视频剧本,轻轻松松获得百万点赞
  18. 数据挖掘技术的算法与应用
  19. LeetCode 981. 基于时间的键值存储 / 274. H 指数 / 275. H 指数 II
  20. C语言const和volatile关键字

热门文章

  1. 都是“工作惯性”惹的祸
  2. web安全漏洞之CSRF
  3. Python开发【第三篇】:文件操作与函数
  4. vue 2.x项目 vue-qriously 生成二维码并下载、cliploard复制粘贴
  5. kvm虚拟化框架结构层次梳理及图形化的使用
  6. placeholder的样式设置
  7. Know about RDBMS market share
  8. ProGuard的返回错误代码1。异常的解决方法
  9. SNDA(上海)招聘中、高级ASP.NET开发人员(2名)
  10. 智能合约的核心思想、语法重点、编程模式、示例、规范及架构