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. 项目总结——深入浅出socket网络编程

    前言: 为什么会有如题的概念呢,我想对于没有主动听说过socket网络编程的人来说读到题目可能就已经蒙头了,为了很好的让大家进入场景,首先说一下一个需要用到这点东西的业务需求. 首先大家应该明确的是s ...

  2. XML数据的读取—数据库配置文件

    数据库配置文件(config.xml) <?xml version="1.0" encoding="utf-8"?> <configurati ...

  3. <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalVal ...

  4. mysql索引和缓存

    mysql有缓存,缓存的设置见[转]MySql查询缓存机制

  5. jQuery节点操作,jQuery插入节点,jQuery删除节点,jQuery Dom操作

    一.创建节点 1 var box = $('<div>节点</div>'); //创建一个节点,或者var box = "<div>节点</div& ...

  6. PHP Calendar 函数

    PHP 5 Calendar 函数 函数 描述 cal_days_in_month() 针对指定的年份和历法,返回一个月中的天数. cal_from_jd() 把儒略日计数转换为指定历法的日期. ca ...

  7. jvm参数设置

    -Xss: 栈大小 -Xms:堆初始化大小-Xmx:堆最大大小-XX:NewSize=n:设置伊甸区大小-XX:NewRatio=n:年轻代与年老代比值.如:为3,表示年轻代与年老代比值是1:3,   ...

  8. linux初识-02常用命令

    文件目录操作命令 ls 现实文件和目录列表 ls -l 列出文件的详细信息 ls -a 列出当前目录所有文件 包括隐藏的文件 mkdir 创建目录 -p 父目录不存在的情况下先生成父目录 cd 切换目 ...

  9. Canvas -画图 关键字

    颜色.样式和阴影 属性 描述 fillStyle 设置或返回用于填充绘画的颜色.渐变或模式 strokeStyle 设置或返回用于笔触的颜色.渐变或模式 shadowColor 设置或返回用于阴影的颜 ...

  10. 类似a:hover的伪类的注解

    a:link { font-size: 14pt; text-decoration: underline; color: blue; } /*设置a对象在未被访问前的样式表属性 .*/ a:hover ...