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. 迷你版Deferred

    直接贴代码: /** * 迷你版的deferred */ function Deferred(func) { if (this instanceof Deferred === false) { ret ...

  2. [译]Mongoose指南 - Document

    更新 有几种方式更新document. 先看一下传统的更新方法 Tank.findById(id, function(err, tank){ if(err) return handleError(er ...

  3. LR测试登陆后进行的操作时 绕过登录

    oadrunner web_add_cookie web_add_cookie 这个的函数原来真的能过逃过登录,哈哈,这个苦苦纠结我的问题呀. 函数原型:int web_add_cookie( con ...

  4. 从程序员到CTO的Java技术路线图

    转自:http://zz563143188.iteye.com/blog/1877266 企业级项目实战地址:http://zz563143188.iteye.com/blog/1825168 开发资 ...

  5. 一种map容器遍历的方法

    遍历算法是一种很常见而且非常重要的算法,我们用map容器的时候可能用的比较多的是查找,我今天才第一次要用到遍历.下面举个例子就知道了. map<string,string> mp; str ...

  6. 关于JavaScript的浅拷贝和深拷贝

    在 JS 中有一些基本类型像是Number.String.Boolean,而对象就是像这样的东西{ name: 'Larry', skill: 'Node.js' },对象跟基本类型最大的不同就在于他 ...

  7. Android 数据存储之 SQLite数据库存储

    ----------------------------------------SQLite数据库---------------------------------------------- SQLi ...

  8. echarts

    ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10/11 ...

  9. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)解题报告

    最近参加了很多CH上的比赛呢~Rating--了..题目各种跪烂.各种膜拜大神OTZZZ T1珠 描述 萌蛋有n颗珠子,每一颗珠子都写有一个数字.萌蛋把它们用线串成了环.我们称一个数字串是有趣的,当且 ...

  10. leetcode:32 最长有效括号

     题目: 给一个包含了'(' 和 ')'的字符串,求出其中最长有效括号的长度. 做题情况:自己做出来,但做了较长的时间. 思路:可以算得穷举法的时间复杂度为O(n^3).虽然这题求的是最长的长度,但是 ...