Sort a linked list in O(n log n) time using constant space complexity.

思路:

用归并排序。设输入链表为S,则先将其拆分为前半部分链表A,后半部分链表B。注意,要把A链表的末尾置为NULL。即要把链表划分为两个独立的部分,防止后面错乱。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; 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;
ListNode * p = head;
int n = ;
while(p->next != NULL){n++; p = p->next;}
return MergeSort(head, n);
} ListNode * MergeSort(ListNode * head, int n)
{
if(n == )
return NULL;
else if(n == )
{
return head;
}
else
{
int m = n / ;
//下面这种划分的方法很挫 应用后面大神的方法
ListNode * headb = head;
ListNode * p = NULL;
int k = ;
while(k < m + )
{
p = headb;
headb = headb->next;
k++;
}
p->next = NULL; ListNode * A = MergeSort(head, m);
ListNode * B = MergeSort(headb, n - m);
return Merge(A, B);
}
} ListNode * Merge(ListNode * A, ListNode * B)
{
ListNode * C, * head;
if(A->val < B->val)
{
C = A; A = A->next;
}
else
{
C = B; B = B->next;
}
head = C; while(A != NULL && B != NULL)
{
if(A->val < B->val)
{
C->next = A; A = A->next;
}
else
{
C->next = B; B = B->next;
}
C = C->next;
} if(A != NULL)
{
C->next = A;
}
else
{
C->next = B;
}
return head;
} void createList(ListNode * &head)
{
int n;
cin >> n;
if(n != )
{
head = new ListNode(n);
createList(head->next);
}
}
}; int main()
{
Solution s;
ListNode * L = NULL;
s.createList(L); ListNode * LS = s.sortList(L); return ;
}

大神精简版代码,关键注意划分过程

ListNode *sortList(ListNode *head) {
if (head == NULL || head->next == NULL)
return head; // find the middle place
ListNode *p1 = head;
ListNode *p2 = head->next;
while(p2 && p2->next) {
p1 = p1->next;
p2 = p2->next->next;
}
p2 = p1->next;
p1->next = NULL; return mergeList(sortList(head), sortList(p2));
} ListNode *mergeList(ListNode* pHead1, ListNode* pHead2) {
if (NULL == pHead1)
return pHead2;
else if (NULL == pHead2)
return pHead1; ListNode* pMergedHead = NULL; if(pHead1->val < pHead2->val)
{
pMergedHead = pHead1;
pMergedHead->next = mergeList(pHead1->next, pHead2);
}
else
{
pMergedHead = pHead2;
pMergedHead->next = mergeList(pHead1, pHead2->next);
} return pMergedHead;
}

【leetcode】Sort List (middle)的更多相关文章

  1. 【leetcode】Sort Colors(middle)☆

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  2. 【LeetCode】Sort Colors 解题报告

    [题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  3. 【Leetcode】Sort List JAVA实现

    Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...

  4. 【LeetCode】 sort list 单清单归并

    称号:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排 ...

  5. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  6. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  7. 【leetcode】Subsets II (middle) ☆

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  8. 【leetcode】Sort List

    Sort List Sort a linked list in O(n log n) time using constant space complexity.   需要采用归并排序对链表进行操作. ...

  9. 【leetcode】Combination Sum (middle)

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

随机推荐

  1. Backbone事件模块源码分析

    事件模块Backbone.Events在Backbone中占有十分重要的位置,其他模块Model,Collection,View所有事件模块都依赖它.通过继承Events的方法来实现事件的管理,可以说 ...

  2. 2015年12月03日 GitHub入门学习(五)Markdown语法简介

    Markdown一种标记语言,语法简洁,不像Word或Pages有大量排版.字体设置.常用的标记符号不超过十个.被大量写作爱好者.撰稿人.作家所青睐. 一.Markdown的优点 专注你的文字内容而不 ...

  3. 电脑开机黑屏,显示Reboot and Select proper boot device!

    “reboot and select proper boot device or insert boot media in selected boot device and press a key” ...

  4. GCD与NSOperationQueue

    1> GCD是纯C语言的API,NSOperationQueue是基于GCD的OC版本封装 2> GCD只支持FIFO(先入先出)的队列,NSOperationQueue可以很方便地调整执 ...

  5. 新年新技术:HTTP/2

    新的一年,项目也要带着发展的眼光往前走,得跟上潮流,当然前提是自己真的用的上. 用的上用不上都得先简单了解下. 2月下旬Google发布了首个基于HTTP/2的RPC框架GRPC,它是基于HTTP/2 ...

  6. Ubuntu 开机进入命令行模式

    1.修改配置 sudo vim /etc/default/grub 把 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" 改为 GRUB_CMDL ...

  7. jQuery Colorbox插件

    http://www.open-open.com/lib/view/open1338084606042.html jQuery Colorbox是一款非常好的内容播放插件.它集弹出层.幻灯片播放功能于 ...

  8. An error in projects

    Error能使系统产生Failure从而导致系统不能达到所需的功能. 曾经,做一个关于酒店管理系统的项目.因为数据库表主外键的连接错误,当对页面的添加桌位功能进行测试时,不能正确的添加. 后通过逐行对 ...

  9. Unity手游之路<一>C#版本Protobuf

    http://blog.csdn.net/janeky/article/details/17104877 个游戏包含了各种数据,包括本地数据和与服务端通信的数据.今天我们来谈谈如何存储数据,以及客户端 ...

  10. auto与decltype

    今天搜狗笔试的一道选择题,原题给忘了,但记得所考的知识点.知识点很基础,但很容易忽视. 具体内容可参考C++ Primer. auto :变量取auto后,其所对应的类型        auto一般会 ...