LeetCode—-Sort List
LeetCode—-Sort List
Question
Sort a linked list in O(n log n) time using constant space complexity.
Solution
看到对链表的排序,时间复杂度O(n log n),首先想到的就是归并排序。 但是这里其中有两个技巧:
- 就是将两个链表分开的时候,用到了fast-slow法,这是在处理链表分治,也就是找中间节点的一种有效方法。
- 还有就是merge的过程,也用到了递归的方式。
Code
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *sortList(ListNode *head) {
if (head == NULL)
return NULL;
if (head->next == NULL)
return head;
ListNode* p1 = head;
ListNode* p2 = head;
ListNode* pre = head;
// fast-slow 法
while (p2 != NULL && p2->next != NULL) {
pre = p1;
p1 = p1->next;
p2 = p2->next->next;
}
pre->next = NULL;
ListNode* L1 = sortList(head);
ListNode* L2 = sortList(p1);
return merge(L1, L2);
};
// merge 链表的方法,也用到了递归
ListNode* merge(ListNode* L1, ListNode* L2) {
if (L1 == NULL)
return L2;
if (L2 == NULL)
return L1;
if (L1->val <= L2->val) {
L1->next = merge(L1->next, L2);
return L1;
} else {
L2->next = merge(L1, L2->next);
return L2;
}
}
};
LeetCode—-Sort List的更多相关文章
- [LeetCode] Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
- [LeetCode] Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode]Sort Colors @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- LeetCode: Sort List 解题报告
Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...
- LeetCode Sort List 链表排序(规定 O(nlogn) )
Status: AcceptedRuntime: 66 ms 题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序.由时间复杂度想到快排.归并这两种排序.本次用的是归并排序.递归将链表的规模不 ...
- [LeetCode] Sort Transformed Array 变换数组排序
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- Leetcode: Sort Transformed Array
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- leetcode sort List
Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-l ...
随机推荐
- Android中Bitmap、Drawable、byte[]转换
public byte[] getBitmapByte(Bitmap bitmap){ ByteArrayOutputStream out = new ByteArrayOutputStream(); ...
- hdu1066(经典题)
求N个数阶乘末尾除0后的数值. 主要的难点在于要把这个N个数所含的2和5的队数去掉. 网上方法很多很好. 不多说 Last non-zero Digit in N! Time Limit: 2000/ ...
- Openstack块存储cinder安装配置
openstack service create --name cinderv2 \ --description "OpenStack Block Storage" volumev ...
- 本地代码推送到github仓库
git 初始化 cd 到需要提交的项目目录下,执行git init 配置用户名和邮箱 git config --global user.name "codingID" git co ...
- 大话Python程序的命名规范
1.全局变量名: 全部大写 MY_GLOBAL_VAR 2. 类名: 首字母大写,总是使用首字母大写单词串,如MyClass,内部类可以使用额外的前导下划线: 3.普通变量,普通函数名,文件名: 全部 ...
- MFC DLL获取当前路径
.首先定义此获取模块的静态方法 #if _MSC_VER >= 1300 // for VC 7.0 // from ATL 7.0 sources #ifndef _delayimp_h ex ...
- time/datetime/random/string/os/sys/shutil/zipfile/tarfile - 总结
time 模块: time.time() #时间戳 time.localtime() #当前时间对象元组 time.localtime(123123) #根据时间戳的时间对象 time.mktime( ...
- PyMongo和MongoEngine
参见 http://stackoverflow.com/questions/5712857/pymongo-vs-mongoengine-for-django https://api.mongodb. ...
- python常见模块之collections模块
一.模块简介 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict.namedtu ...
- SQL JOIN使用方法
(转自W3School相关教程:http://www.w3school.com.cn,W3School是不错的在线教程,简洁高效!) 下面列出不同的SQL JOIN类型,以及他们之间的差异: JOIN ...