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. tomcat  nginx  证书切换

    1. 导出公钥 keytool -export -alias tomcat -keystore <you jks>wsriakey.keystore -file <outputfil ...

  2. 7个GIF动图帮你瞬间理解三角函数

    7个GIF动图帮你瞬间理解三角函数 蝌蚪五线谱 百家号04-2120:53 图片来源:IMGUR 三角函数是数学中研究三角形的一个分支,专门阐述三角形的角度和对应边的关系. 有趣的是,定义边角关系的三 ...

  3. [LeetCode系列]子集枚举问题[无重复元素]

    给定一组数(未排序), 求它们的所有组合可能. 如给定{1 2 3}, 返回: [ [] [1] [2] [3] [1 2] [1 3] [2 3] [1 2 3] ] 算法思路: 对数组排序, 从小 ...

  4. win10下安装并启动zookeeper

    下载直接到zk的官网(zookeeper.apache.org)即可,点击右边的Releases,在Download下再点Download进入镜像下载页面,在给出的链接列表里选择一个镜像地址,进去后选 ...

  5. Linux 根文件系统目录结构

    /:根目录 /bin:linux的常用命令 /sbin:linux的常用命令 /lib:库文件(so.elf) /etc:系统配置文件和脚本文件 /sys:驱动相关的信息 /dev:设备节点目录 /p ...

  6. 在64位的UBUBTU 服务器 ***

    前言: 安装/重装系统,U盘启动盘不稳定,建议使用USB-CDROM启动 如果是重装系统,要记录好硬盘的信息和职能: fdisk -l lsblk blkid /etc/fstab mkfs.ext3 ...

  7. 带ssl的websocket例子

    还是在那个websocket_demo的例子 rebar-creator create-app websocket_demo tree一下看看大概目录 ├── cert │   ├── cowboy- ...

  8. ubuntu下面搭建SolrCloud集群

    首先要先把ubuntu环境搭建好,配置好静态IP,我这边配置的是3台机子,solr搭建集群至少是2台. 192.168.0.15  主机 192.168.0.16  从机 192.168.0.17  ...

  9. 安全人员常用的python库

    如果你对漏洞挖掘.逆向工程分析或渗透测试感兴趣的话,我第一个要推荐给你的就是Python编程语言.Python不仅语法简单上手容易,而且它还有大量功能强大的库和程序可供我们使用.在这篇文章中,我们会给 ...

  10. http的含义

    HTTP是超文本传输协议,是客户端浏览器或其他程序与Web服务器之间的应用层通信协议.在Internet上的Web服务器上存放的都是 超文本信息,客户机需要通过HTTP协议传输所要访问的超文本信息.H ...