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. C# Winform 脱离 Framework (一)

    Linker是一个命令行工具,它以将我们的.net程序生成可脱离.net framework环境运行的程序 . Linker不支持中文的路径,在程序中也不能有中文的标识符. Linker 有2种部署方 ...

  2. 2015年11月26日 Java基础系列(四)class的定义,继承和实现interface

    序,类的设计是JAVA操作的核心,面对对象思想中一切皆对象. 一.类定义中的变量 静态成员变量,为类所有,称为类变量:只有一份,编译时即分配值,使用关键字static声明. 非静态成员变量,每个实例一 ...

  3. [译]git add

    git add git add命令把工作目录下面的有修改的文件添加的index(staging)里面去. git add告诉Git你想在下次commit的时候把什么文件包含进去. 但是, git ad ...

  4. QT编写上位机程序一定要初始化变量以及谨慎操作指针

    背景: 在编写QT上位机界面时,界面在运行的时候经常出现卡死或者直接挂掉的怪现象. 正文: 上位机有个函数为check_receive():该函数的作用为定时调用循环检测USB是否有数据.若有,则将信 ...

  5. flexbox-CSS3弹性盒模型flexbox完整版教程

    原文链接:http://caibaojian.com/flexbox-guide.html flexbox-CSS3弹性盒模型flexbox完整版教程 A-A+ 前端博客•2014-05-08•前端开 ...

  6. C语言各种标准的

    [K&R C] 1978 年,Dennis Ritchie 和 Brian Kernighan 合作推出了<The C Programming Language>的第一版(按照惯例 ...

  7. 0821找不到Command Line Utility的解决方案

    在Object-C基础教程中写到,要求选择Xcode中Mac OS X - Command Line Utility - Foundation Tool 但在Xcode4.5中Mac OS X中没有C ...

  8. Android坐标系统

     1 背景 去年有很多人私信告诉我让说说自定义控件,其实通观网络上的很多博客都在讲各种自定义控件,但是大多数都是授之以鱼,却很少有较为系统性授之于渔的文章,同时由于自己也迟迟没有时间规划这一系列文章, ...

  9. ubuntu 安装mysql

    1.检查是否已经安装mysql netstat -tap | grep mysql 2.安装mysql 2.1 先更新下软件列表 :sudo apt-get update 2.2 安装mysql :s ...

  10. hibernate 学习知识总结

    1.最近用hibernate 学会很多知识,总结如下: (1)数据库表格已经设置默认值,在进行数据插入的时候,bean里面不赋值的话,插入之后该字段依旧是null 是因为hibernate默认插入和更 ...