148. Sort List (List)
Sort a linked list in O(n log n) time using constant space complexity.
法I:快排。快排的难点在于切分序列。从头扫描,碰到>=target的元素,停止;从第二个字串扫描,碰到<=target的元素停止;交换这两个元素。这样的好处是:当数据元素都相同时,也能控制在logn次递归(否则需要O(n))。另外,要注意避免子序列只剩两个相等元素时的死循环。
/**
* 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 || head->next==NULL) return head; //only one element ListNode* dummyHead1 = new ListNode();
ListNode* dummyHead2 = new ListNode();
ListNode* fastNode = dummyHead1;
ListNode* slowNode = dummyHead1;
ListNode* cur1, *cur2;
int tmp;
dummyHead1->next = head; //fast, slow pointer to find the middle point
while(fastNode->next){
fastNode = fastNode->next;
if(fastNode->next) fastNode = fastNode->next;
else break;
slowNode = slowNode->next; //slowNode always point to the element before center(odd number)
// or the left center (even number)
} //partition the sequence into two halves
dummyHead2->next = slowNode->next;
slowNode->next=NULL;
cur1 = dummyHead1;
cur2 = dummyHead2->next;
while(cur1->next&&cur2->next){
//stop when find an element in first half, value of whihch >= target
while(cur1->next && cur1->next->val < dummyHead2->next->val) cur1 = cur1->next;
//stop when find an element in second half, value of which <= target
while(cur2->next && cur2->next->val > dummyHead2->next->val) cur2 = cur2->next;
if(!cur1->next || !cur2->next ) break;
tmp = cur1->next->val;
cur1->next->val = cur2->next->val;
cur2->next->val = tmp;
cur1 = cur1->next;
cur2 = cur2->next; }
while(cur1->next){
//stop when find an element in first half, value of which > target
//>= may lead to endless recursion if two equal elements left
while(cur1->next && cur1->next->val <= dummyHead2->next->val) cur1 = cur1->next;
if(!cur1->next) break;
cur2->next = cur1->next;
cur1->next = cur1->next->next;
cur2 = cur2->next;
cur2->next = NULL;
}
while(cur2->next){
//stop when find an element in second half, value of which < target
//<= may lead to endless recursion if two equal elements left
while(cur2->next && cur2->next->val >= dummyHead2->next->val) cur2 = cur2->next;
if(!cur2->next) break;
cur1->next = cur2->next;
cur2->next = cur2->next->next;
cur1 = cur1->next;
cur1->next = NULL;
} //cascade two halves
head = sortList(dummyHead1->next);
cur2 = sortList(dummyHead2->next);
if(head==NULL) return cur2;
cur1 = head;
while(cur1->next){
cur1 = cur1->next;
}
cur1->next = cur2;
return head;
} };
法II: 归并排序。由于是List,归并排序的好处是不用额外申请O(n)的空间
/**
* 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 || head->next==NULL) return head; //only one element ListNode* dummyHead1 = new ListNode();
ListNode* dummyHead2 = new ListNode();
ListNode* fastNode = dummyHead1;
ListNode* slowNode = dummyHead1;
ListNode* cur1, *cur2, *cur;
dummyHead1->next = head; //fast, slow pointer to find the middle point
while(fastNode->next){
fastNode = fastNode->next;
if(fastNode->next) fastNode = fastNode->next;
else break;
slowNode = slowNode->next; //slowNode always point to the element before center(odd number)
// or the left center (even number)
}
dummyHead2->next = slowNode->next;
slowNode->next = NULL; //recursion
cur1 = sortList(dummyHead1->next);
cur2 = sortList(dummyHead2->next); //merge
cur = dummyHead1;
while(cur1 && cur2){
if(cur1->val <= cur2->val){
cur->next = cur1;
cur1 = cur1->next;
}
else{
cur->next = cur2;
cur2 = cur2->next;
}
cur = cur->next;
}
if(cur1){
cur->next = cur1;
}
else{
cur->next = cur2;
}
return dummyHead1->next;
} };
148. Sort List (List)的更多相关文章
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- 148. Sort List - LeetCode
Solution 148. Sort List Question 题目大意:对链表进行排序 思路:链表转为数组,数组用二分法排序 Java实现: public ListNode sortList(Li ...
- [LeetCode] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- Java for LeetCode 148 Sort List
Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...
- 148. Sort List -- 时间复杂度O(n log n)
Sort a linked list in O(n log n) time using constant space complexity. 归并排序 struct ListNode { int va ...
- 148. Sort List
Sort a linked list in O(n log n) time using constant space complexity. 代码如下: /** * Definition for si ...
- leetcode 148. Sort List ----- java
Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...
- [LeetCode] 148. Sort List 解题思路
Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...
- 【leetcode】148. Sort List
Sort a linked list in O(n log n) time using constant space complexity. 链表排序可以用很多方法,插入,冒泡,选择都可以,也容易实现 ...
- 148. Sort List (java 给单链表排序)
题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...
随机推荐
- list.ForEach的用法
Templist.ForEach(o => { var isSel = ReviewerFileRelationService.Where(s => s.PackageFileId == ...
- ubuntu15.10下code::blocks设置运行窗口为gnome命令行
code::blocks编译运行C++程序(F9)默认出现的运行串口在有鼠标的情况下进行粘贴还是很方便的,只要按下鼠标滑轮,位与剪切板中的数据就能粘贴到运行串口中. 但是对于用笔记本而且没有鼠标地童鞋 ...
- 从wiresharp看tcp三次握手
我们知道,传输层是OSI模型中用户进行数据传输的分层,目前仅有TCP和UDP两种协议可用.TCP为了进行传输控制,引入了三次握手机制,以确保通信连接的建立.道理很简单,我们跟别人打电话聊天时,对方拿起 ...
- IMAP简单研究
IMAP的相关详细介绍: http://www.imapwiki.org/ClientImplementationhttp://tools.ietf.org/html/rfc3501 1.连接服务器 ...
- linux(7)
第十七单元 Samba服务 [本节内容]1. 掌握samba的功能: samba是一个网络服务器,用于Linux和Windows之间共享文件.2. 掌握samba服务的启动.停止.重启service ...
- github打不开问题
修改host 185.31.17.184 github.global.ssl.fastly.net 207.97.227.239 http://github.com 65.74.177.129 htt ...
- 第四章.使用ant编译hadoop eclipse插件
从hadoop 0.20.203以后,hadoop的发布包里,不再对eclipse插件进行jar包发布,而是给出了打包的代码,需要各位开发人员自己进行打包和设置.我们打的包必须跟自己使用的hadoop ...
- canvas之画矩形
<canvas id="canvas" width="600" height="500" style="background ...
- Cookie与Session的复习
Cookie Cookie是HTTP协议制定的.先由服务器保存Cookie到浏览器,再下次浏览器请求服务器时把上一次请求得到Cookie再归还给服务器.由服务器创建保存到客户端浏览器的一个键值对(由服 ...
- 移动app非功能测试点收集
非功能测试 移动app测试的另一重要方面是移动app的非功能需求.移动app在推出市场或进行进一步开发前,移动测试员有许多需要测试的问题. 早期开发阶段要进行的第一个测试应该是实用性测试.通常是由al ...