[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-> ...
随机推荐
- js中的for...in循环机制
1) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.o ...
- SpringMVC04controller中定义多个方法
public class MyController extends MultiActionController { // 新增 方法修饰符要是public public ModelAndView ad ...
- C#学习(三)
通过类创建对象的过程称为类的实例化 匿名类型提供了一种方便的方法,可用来将一组只读属性封装到单个对象中,而无需首先显式定义一个类型. 要将匿名类型或包含匿名类型的集合作为参数传递给某一方法,可将参数作 ...
- Objective-C 笔记一(HelloWorld)
作为一个果粉And程序员,奔着对OC浓厚的兴趣,开始学习IOS.并以后也想从事IOS开发工作.并将自己的学习记录下来,俗话说的好,不会总结的程序猿,不是好程序员! Xcode可以在AppStore里下 ...
- ajax调用action后返回list给list.jsp,显示为xml文档
struts2中使用的是map来保存数据的,所以这里绑定的值是key和value1 <?xml version="1.0" encoding="UTF-8" ...
- LINQ对List列表随机排序,取N条数据
List<Art_Search> artList=new List<Art_Search>(); artList=artList.OrderBy(s => Guid.Ne ...
- Python----定义
变量的定义: 变量第一次出现不是声明类型就是赋初值,才能后续使用. 函数的定义: ''' 函数的返回值不用声明类型 函数参数值最好赋一个类型值,例如整型赋值0,列表[] 函数名后面必须跟: ''' d ...
- c++中 cin、cin.get()、cin.getline()、cin.getchar()的区别
①cin>>:无法接收空格.Tap键且以空格.Tap键.回车符为分隔符: ②cin.get( ):可以接收空格.Tap键且以回车符为结束符: 一:可输入单个字符 格式: char ch; ...
- phpcms二次开发步骤
文件目录结构 根目录 | – api 接口文件目录 | – caches 缓存文件目录 | – configs 系统配置文件目录 | – caches_* 系统缓存目录 | – phpcms phpc ...
- PHPCMS V9 简单的二次开发
更多二次开发技巧,查看phpcms系统帮助 ,前台模板解析后的缓存 caches\caches_template\default 前台控制类index.php,前台标签类*_tag.class.php ...