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) ...
随机推荐
- c# winform委托的使用
可参考http://bbs.csdn.net/topics/390377875中使用new Action<>的使用方式,替代delegate的申明. public delegate voi ...
- bzoj 3158 千钧一发——网络流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3158 发现偶数之间一定满足第二个条件:奇数之间一定满足第一个条件 ( \( (2m+1)^{ ...
- 原生 Javascript 编写五子棋
原文地址:原生 Javascript 编写五子棋 博客地址:http://www.extlight.com 一.背景 近一个月没写 Javascript 代码,有点生疏.正好浏览网页时弹出五子棋的游戏 ...
- 关于CSS单位:rem vh vw vmin vmax
rem(root em) 如果你给body设置了font-size字体大小,那么body的任何子元素的1em就是等于body设置的font-size demo: body { font-size: ...
- JavaScript Promise启示录--(转)
本博文转至:http://www.csdn.net/article/2014-05-28/2819979-JavaScript-Promise [编者按]JavaScript是一种基于对象和事件驱动并 ...
- HBase之二:Hbase优化
1. 预先分区 默认情况下,在创建 HBase 表的时候会自动创建一个 Region 分区,当导入数据的时候,所有的 HBase 客户端都向这一个 Region 写数据,直到这个 Region ...
- 1095 Cars on Campus
题意:给出N量车的车牌号,进出的时间,进/出状态.然后给出若干个查询,要求计算在每一查询时刻校园内停着的汽车数量,最后输出这一天中停放时间最长的车辆(若车不止一辆,则按字典序输出)以及停放时间.注:查 ...
- 无线加密的多种方法及其区别(WEP WPA TKIP EAP)
无线加密的多种方法及其区别(WEP WPA TKIP EAP) 无线网络的安全性由认证和加密来保证. 认证允许只有被许可的用户才能连接到无线网络: 加密的目的是提供数据的保密性和完整性(数据在传输过程 ...
- 使用jQuery Pagination Plugin实现分页效果
最近使用分页这个基础效果较为频繁,而项目前端页面使用的是纯静态的HTML,自己之前写的JSP中的分页就用不成了:项目中也引入了Bootstrap,本来想使用Bootstrap中的分页样式,但发现其样式 ...
- 【UVA】1595 Symmetry(模拟)
题目 题目 分析 理清思路,上模拟. 代码 #include <bits/stdc++.h> using namespace std; const int maxn=100 ...