[LeetCode] 148. Sort List 解题思路
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 解题思路的更多相关文章
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- Java for LeetCode 148 Sort List
Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...
- [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 ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [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 ...
- [LeetCode] 53. Maximum Subarray 解题思路
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】148. Sort List 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] 147. Insertion Sort List 解题思路
Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...
- [LeetCode] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
随机推荐
- java web mvc思想介绍
1.首先简介一下什么是MVC思想. 在百度百科里面对MVC的说明,MVC全名是Model View Controller.是模型(model)-视图(view)-控制器(controller)的缩写. ...
- hdu 3037 Saving Beans(组合数学)
hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以 ...
- Mysql + keepalived 实现双主热备读写分离【转】
Mysql + keepalived 实现双主热备读写分离 2013年6月16日frankwong发表评论阅读评论 架构图 系统:CentOS6.4_X86_64软件版本:Mysql-5.6.12 ...
- [RxJS] Creation operators: empty, never, throw
This lesson introduces operators empty(), never(), and throw(), which despite being plain and void o ...
- Can a Tomcat docBase span multiple folders?--转
Question: I apologize if this is a poor question, but I'm using Windows and looking to see if there' ...
- js中的for...in循环机制
1) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.o ...
- 使用静态资源设置UI信息
首先建立一个文件存放样式设置(资源字典),所有风格设置都可以这里进行 加入以下代码: <ResourceDictionary xmlns="http://schemas.microso ...
- ASP.NET协作应用集成到trsids身份验证服务器的开发流程
开发Actor协同模块: (参考TRSIDS4.0 协作应用集成手册[asp.net]) ASP.Net协作应用集成到IDS之前,需要开发Actor类实现协作应用回调接口中定义的本地登录.退出.用户信 ...
- 3.RxJava详解
一.RxJava 到底是什么 异步(取代AsyncTask/Handler/XXX/...?) 二.RxJava 好在哪 简洁(逻辑的简洁,.一步一走) 举例: 题目:将文件夹中的图片都取 ...
- SQL 关于有单引号数据更新的问题
要把sql语句中包含有单引号的符号加入到数据库中的做法 )),''','123.com') 很简单就是加入id=''123'' 0'0就可以写成'0''0'