【Sort List】cpp
题目:
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的更多相关文章
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 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~ ...
- POJ 1018 【枚举+剪枝】.cpp
题意: 给出n个工厂的产品参数带宽b和价格p,在这n个工厂里分别选1件产品共n件,使B/P最小,其中B表示n件产品中最小的b值,P表示n件产品p值的和. 输入 iCase n 表示iCase个样例n个 ...
- 【Merge Intervals】cpp
题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...
- 【Interleaving String】cpp
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- 【Combination Sum 】cpp
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- leetcode 【 Sort List 】 python 实现
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码:oj 测试通过 Runtime: 372 m ...
随机推荐
- CSS中属性position位置详解功能讲解与实例分析
position有五个值:static.relative.absolute.fixed.inherit. static 是默认值.就是按正常的布局流从上到下从左到右布局,平常我们做网页制作时,没有指定 ...
- Knockout.Js官网学习(visible绑定)
前言 让visible绑定到DOM元素上,使得该元素的hidden或visible取决于绑定的值. 简单的绑定 首先还是先定义一个ViewModel var AppViewModel = { shou ...
- Java应用架构的演化之路
Java应用架构的演化之路 当我们架设一个系统的时候通常需要考虑到如何与其他系统交互,所以我们首先需要知道各种系统之间是如何交互的,使用何种技术实现. 1. 不同系统不同语言之间的交互 现 在我们常见 ...
- IntelliJ IDEA 13破解(JRebel 5.6.3a破解)
首先安装IntelliJ 13,记得要下载Ultimate Edition版本,不然就不需要破解了.. 安装到本地,然后进行一些配置(这一步可以不要,但是考虑到以后换系统可以省事,推荐做) 打开{in ...
- delphi启动 EditLineEnds.ttr 被占用问题
碰到这个问题我也是无语了,每次关掉Delphi后就不能打开了,折腾了半天,网上的方法也没有搞定.最后,找到这个链接(网页如下所示),下载里面的东西就搞定了.打不开的可以向我要.895569369@qq ...
- 可以获取get post url 传递参数的统一方法
public static string objRequest(string requestName) { object obj = HttpContext.Current.Request[reque ...
- 【转】 BSS段 数据段 代码段 堆栈 指针 vs 引用
原文:http://blog.csdn.net/godspirits/article/details/2953721 BSS段 数据段 代码段 堆栈 (转+) 声明:大部分来自于维基百科,自由的百科全 ...
- Unity Scripting Tutorials 要点记录
(搬运自我在SegmentFault的博客) 这几天通过Unity官网的Unity Scripting Tutorials的视频学习Unity脚本,观看的过程中做了记录.现在,整理了一下笔记,供自己以 ...
- LaTeX中无法显示中文问题
- Secondary IP Addressing
Secondary IP Addressing secondary IP addressing. Secondary addressing uses multiple networks or subn ...