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)的更多相关文章

  1. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

  2. 148. Sort List - LeetCode

    Solution 148. Sort List Question 题目大意:对链表进行排序 思路:链表转为数组,数组用二分法排序 Java实现: public ListNode sortList(Li ...

  3. [LeetCode] 148. Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...

  4. Java for LeetCode 148 Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...

  5. 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 ...

  6. 148. Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 代码如下: /** * Definition for si ...

  7. leetcode 148. Sort List ----- java

    Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...

  8. [LeetCode] 148. Sort List 解题思路

    Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...

  9. 【leetcode】148. Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 链表排序可以用很多方法,插入,冒泡,选择都可以,也容易实现 ...

  10. 148. Sort List (java 给单链表排序)

    题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...

随机推荐

  1. qt ui程序使用Linux的文件操作open、close (转)

    原文地址:qt ui程序使用Linux的文件操作open.close 作者:kjpioo 提出这个问题是因为在qt的QWidget类型的对象中,close()函数会和QWidget::close()冲 ...

  2. mysql用触发器同步表

    一.先复制表 : and DATE = '2016-09-26' or DATE = '2016-09-27'; 二.创建插入数据时的[触发器]  [在phpmyadmin 运行时记得要修改语句定界符 ...

  3. jeecg中树形显示的用法

    1.GoodsController中显示的方法如下: @RequestMapping(params = "goodsgrid") @ResponseBody public Obje ...

  4. HDOJ5883(欧拉路)

    The Best Path Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  5. poj 3977 Subset(折半枚举+二进制枚举+二分)

    Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 5721   Accepted: 1083 Descripti ...

  6. Java-Runoob:Java 方法

    ylbtech-Java-Runoob:Java 方法 1.返回顶部 1. Java 方法 在前面几个章节中我们经常使用到 System.out.println(),那么它是什么呢? println( ...

  7. Vue踩坑记录册

    1.vue-cli+webpack项目 修改项目名称 解决办法: 1 删除 node_modules 文件夹(如果修改项目名称,需要在在package.json中修改对应的name) 2 重新安装依赖 ...

  8. [C++]复制构造函数、赋值操作符与隐式类类型转换

    问题:现有类A定义如下: class A{public:        A(int a)                            //构造函数        {              ...

  9. 第十章 Secret & Configmap (中)

    10.3 在Pod中使用Secret 10.3.1 Volume方式 apiVersion: v1 kind: Pod metaata: name: mypod spec: containers: - ...

  10. line 1: syntax error: unexpected word (expecting ")")

    编译出来的程序在arm平台上运行时,出现下面的错误. / # wpa_supplicant -B -c/etc/wpa_wpa2.conf  -iwlan0 /bin/wpa_supplicant: ...