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. Php面向对象 – 单例模式

    Php面向对象 – 单例模式 保证类仅仅有一个实例 1.    怎样能够解决一个类能够被无限地实例化? New,就能实例化一次,怎么去限制,用户不能无限次地new? 将构造方法私有化.全部外部的new ...

  2. UITableView显示不全

    先上图(不全图片): 正确图片: 原因例如以下: 1.在tableView的父视图的freme问题. 2.tableView本身的frame问题.大小依据自己的实际情况改过来就OK了 希望能够帮助到你 ...

  3. CCDictionary&CCArray执行retain()重要点

    CCDictionary也需要执行retain(),否则则跟CCArray,返回则释放对象. 在Lua中,忘记了retain(),导致一些出现gCCDictionary:objectForKey(ke ...

  4. C# 面向对象 , 类与对象

    一,类的字段 类的字段, 就是类里面的 数据. 二,类的方法 1 , 函数的重载 , 如何判断:  是由两个或多个同名函数组成的,但是函数要有不同的参数.或个数.(参数  是函数的输入的东西) shu ...

  5. 解释DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

    解释DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci 在创建数据库的时候,经常用到一句:CREATE DATABASE `tpcms` DEFAUL ...

  6. 用CALayer实现聚光灯效果

    效果图: 代码部分: #import "ViewController.h" @interface ViewController () @property (nonatomic, s ...

  7. Covariant Returen Types(协变返回类型)

    基类virtual func返回类型为某个类(class Super)的ptr或ref,子类重写的virtual func返回类型可改为该类子类(class Sub : public Super)的p ...

  8. 记一次MySQl 安装1067错误

    1.今天阿里云windows server 2008 r2服务器上安装mysql,配置完发现无法启动mysql服务(并发设置的是500),查询windows日志提示 Unknown/unsupport ...

  9. 微信 回复多图文 借助php框架

    private function replyMostPhoto($data,$arr){$this->logger("已经到达回复多图文!".$arr[0]['Title'] ...

  10. xml配置与使用

    php100:89:xml常识知识补充 xml常识知识补充XML(即可扩展标记语言,它与HTML一样,都是标准通用标记语言.Xml是Internet环境中跨平台的,依赖于内容的技术.扩展标记语言XML ...