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

问题:对一个单列表排序,要求时间复杂度为 O(n*logn),额外空间为 O(1)。

O(n*logn) 时间排序算法,无法是 quick sort, merge sort, head sort。quick sort 需要灵活访问前后元素,适合于数组,merge sort 只需要从左到右扫过去即可,可用于列表结构。

  • 当列表元素个数大于2时,将列表拆分为左右对半的两个子列表,对左右子列表分别排序,然后合并。
  • 当列表元素个数小于等于2 时,直接对列表元素比较排序。

第一步中的合并操作,实际上另一个LeetCode题目 Merge Two Sorted Lists

需要注意的是,由于是单向列表,为了方便操作元素位置,每次比较操作比较的是指针的下一个元素,详情见 sortmerge 函数。

     /**
* p 表示父节点
* ll, lr 分别表示左半列表的父节点,右半列表的父节点
*
*/
ListNode* sortmerge(ListNode* p, int len){ if (len <= ) {
return p;
} if (len == ) {
if (p->next->val > p->next->next->val) {
int tmp = p->next->val;
p->next->val = p->next->next->val;
p->next->next->val = tmp;
}
return p;
} int lenL = len / ;
int lenR = len - lenL; ListNode* ll = p; ll = sortmerge(ll, lenL); ListNode* lr = p; for (int i = ; i < lenL; i++) {
lr = lr->next;
} lr = sortmerge(lr, lenR); while (ll != lr && lenR > ) {
if (ll->next->val <= lr->next->val) {
ll = ll->next;
}else{
ListNode* next2 = lr->next;
lr->next = lr->next->next;
next2->next = ll->next;
ll->next = next2;
lenR--;
}
} return p;
} ListNode* sortList(ListNode* head) { ListNode* node = head; int cnt = ;
while (node != NULL) {
cnt++;
node = node->next;
} ListNode* p = new ListNode();
p->next = head; p = sortmerge(p, cnt); head = p->next; return head;
}

参考资料:

[LeetCode] Sort List, Solution, 水中的鱼

[LeetCode] 148. Sort List 解题思路的更多相关文章

  1. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

  2. Java for LeetCode 148 Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...

  3. [LeetCode] 16. 3Sum Closest 解题思路

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  4. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. [LeetCode] 134. Gas Station 解题思路

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  6. [LeetCode] 53. Maximum Subarray 解题思路

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. 【LeetCode】148. Sort List 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. [LeetCode] 147. Insertion Sort List 解题思路

    Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...

  9. [LeetCode] 148. Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...

随机推荐

  1. linux杂谈(二十):apache服务配置

    1.apache简单介绍 ​ ​我们常常要浏览网页,提供这种服务是apache.提供apache服务的软件是httpd服务. ​ ​Apache支持許多特性,大部分通过编译的模块实现.這些特性從伺服器 ...

  2. [Angular 2] Using a Reducer to Change an Object's Property Inside an Array

    Reducers are also often used for changing a single property inside of other reducers. This lesson sh ...

  3. sqlcipher移植

    一.下载代码 sqlcipher赖openssl库,首先下载openssl: [fulinux@ubuntu ~]$ git clone https://github.com/openssl/open ...

  4. 【剑指offer】和为定值的连续正数序列

    .可是他并不满足于此,他在想到底有多少种连续的正数序列的和为100(至少包含两个数).没多久,他就得到还有一组连续正数和为100的序列:18,19,20,21,22.如今把问题交给你,你能不能也非常快 ...

  5. 完毕port(CompletionPort)具体解释 - 手把手教你玩转网络编程系列之三

       手把手叫你玩转网络编程系列之三    完毕port(Completion Port)具体解释                                                    ...

  6. mac下的home键、end键以及insert键的替代

    最近用android模拟器模拟东西,发现模拟器的home快捷键是键盘上的home键,这让我在windows下很好找,换到mac下找了老半天也没找到,后来才查到是有替代键的,放到这里做备份 home键f ...

  7. Debian 桌面美化

    Debian 桌面美化 安装 gnome-tweak-tool aptitude install gnome-tweak-tool 登陆gnome-look下载主题包 gnome-look上有很多主题 ...

  8. codevs 3693 数三角形

    /* n*m个点中选3个 再排除三点共线 共线分两类 1 在横线或者竖线上 m*C(n,3) n*C(m,3) 2 在对角线上 这个比较麻烦 以为对角线和矩阵是一一对应的 我们转化成求矩阵 并且保证有 ...

  9. Remoting 的“传递的引用”理解

    WCf是集大成者,具有其他微软的很多技术,其中分布式上很多借助于Remoting,所以研究一下Remoting有助于理解WCF 提到Remoting就不得不涉及到MarshalByRefObject这 ...

  10. CSS3 背景

    CSS3包含多个新的背景属性,他们提供了对背景更强大的控制. 本章将学到一下背景属性: background-size background-origin 你也将学到如何使用多重背景图片. 浏览器支持 ...