题目:

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

代码:

/**
* 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 || !head->next ) return head;
ListNode dummy(-);
dummy.next = head;
ListNode *p1=&dummy, *p2=&dummy;
while ( p2 && p2->next && p2->next->next )
{
p1 = p1->next;
p2 = p2->next->next;
}
ListNode *h1 = Solution::sortList(p1->next);
p1->next = NULL;
ListNode *h2 = Solution::sortList(dummy.next);
return Solution::mergeTwo(h1, h2);
}
static ListNode* mergeTwo(ListNode *h1, ListNode *h2)
{
ListNode dummy(-);
ListNode *p = &dummy;
while ( h1 && h2 )
{
if ( h1->val<h2->val )
{
p->next = h1;
h1 = h1->next;
}
else
{
p->next = h2;
h2 = h2->next;
}
p = p->next;
}
p->next = h1 ? h1 : h2;
return dummy.next;
}
};

tips:

单链表时间要求O(nlongn) 且const extra space,可以选择归并排序(另,双向链表适合用快速排序)

第一次没有AC,原因是少考虑一种返回条件,即“head只有一个元素的时候需要直接返回”,修改之后第二次AC了。

===================================================

第二次过这道题,尝试着摸索写出来,一次AC了。

/**
* 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 ) return NULL;
if ( !head->next ) return head;
ListNode dummpy();
ListNode* p1 = &dummpy;
ListNode* p2 = &dummpy;
dummpy.next = head;
while ( p2 && p2->next )
{
p1 = p1->next;
p2 = p2->next->next;
}
ListNode* r = Solution::sortList(p1->next);
p1->next = NULL;
ListNode* l = Solution::sortList(dummpy.next);
return Solution::merge2SortedLists(l,r); }
static ListNode* merge2SortedLists(ListNode* p1, ListNode* p2)
{
ListNode head();
ListNode* p = &head;
while ( p1 && p2 )
{
if ( p1->val < p2->val )
{
p->next = p1;
p1 = p1->next;
}
else
{
p->next = p2;
p2 = p2->next;
}
p = p->next;
}
p->next = p1 ? p1 : p2;
return head.next;
}
};

【Sort List】cpp的更多相关文章

  1. 【Sort Colors】cpp

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

  2. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  3. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  4. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  5. POJ 1018 【枚举+剪枝】.cpp

    题意: 给出n个工厂的产品参数带宽b和价格p,在这n个工厂里分别选1件产品共n件,使B/P最小,其中B表示n件产品中最小的b值,P表示n件产品p值的和. 输入 iCase n 表示iCase个样例n个 ...

  6. 【Merge Intervals】cpp

    题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...

  7. 【Interleaving String】cpp

    题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...

  8. 【Combination Sum 】cpp

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

  9. leetcode 【 Sort List 】 python 实现

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码:oj 测试通过 Runtime: 372 m ...

随机推荐

  1. jquery 读取xml

    <script type="text/javascript" src="jquery/jquery-1.11.3.min.js"></scri ...

  2. Overview Of Portal Registry And Content References

     Portal Registry Each portal is defined by a portal registry.A portal registry has a tree-like struc ...

  3. css3选择器——导图篇

    css3选择器主要有:基本选择器 , 层次选择器,  伪类选择器 ,  伪元素选择器 , 属性选择器 基本选择器  层次选择器 伪类选择器分为 动态伪类选择器, 目标伪类选择器, 语言伪类选择器, U ...

  4. php设计模式之Proxy(代理模式)和Facade(外观)设计模式

    Proxy(代理模式)和Facade(外观)设计模式它们均为更复杂的功能提供抽象化的概念,但这两种实现抽象化的过程大不相同 Proxy案例中,所有的方法和成员变量都来自于目标对象,必要时,该代理能够对 ...

  5. android在程序中打开另一个程序

    在开发android应用的时候,在一些情况下要有前置条件,比如这边所说的要启动时要确保别的应用程序服务已经打开  或者在操作中启动别的应用等. 先来一段google上的代码: 1. 已知包名和类名的情 ...

  6. 每天进步一点--c#基础巩固,事件、委托

    要想技术有所提高,就是把有些问题真正的弄懂弄明白,我从事C#开发两年了,一直对事件委托等概念一知半解,有时候博客园上看看别的大牛的文章,看看懂了就过去了,时间长了又忘了,真正理解还是要自己动手弄些例子 ...

  7. linux 修改系统时间

    首先进入/proc/sys/xen,执行以下命令 [root@test]#cd   /proc/sys/xen[root@test]#echo 1 > independent_wallclock ...

  8. 刀哥多线程之并发队列gcd-05-dispatch_queue_concurrent

    并发队列 特点 以先进先出的方式,并发调度队列中的任务执行 如果当前调度的任务是同步执行的,会等待任务执行完成后,再调度后续的任务 如果当前调度的任务是异步执行的,同时底层线程池有可用的线程资源,会再 ...

  9. poj 2777 Count Color

    题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...

  10. hdu 1872 稳定排序

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1872 稳定排序 Description 大家都知道,快速排序是不稳定的排序方法.如果对于数组中出现的任 ...