【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 ...
随机推荐
- url中文参数解决方案
首先,弄清楚为什么url传递中文会转码或者乱码,以及http头 contentType="text/html; charset=GBK" 的作用. html代码会经过web服务器, ...
- STL使用sort注意的问题
结构体使用sort算法时,重载operator<(..).如果我们按下面这样写 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- js 定位到指定位置
<script> //滚动定位到product function scroll() { var scroll_offset = $(&quo ...
- 用Lambda表达式操作List集合
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 《第一行代码--Android》阅读笔记之广播
广播接收器 1.注册方式 动态注册:在程序中注册,如在Activity里的onCreate()方法中注册 静态注册:在AndroidManifest.xml中注册 2.可接收哪些广播 接收系统消息 ...
- C#中使用官方驱动操作MongoDB
想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动有很多种,如官方提供的,samus. 实现思路大都类似.这里我们先用官方提供的mongo-csharp-dri ...
- Html5元素及基本语法
HTML标签开始标签(opening tag):开放标签结束标签(closing tag):闭合标签 元素定义:HTML元素指的是从开始标签到结束标签的代码(元素以开始标签为起始以借宿标签终止)元素的 ...
- 【转】在delphi中实现控件的拖拽
提示:可以添加一个布尔来控制可否拖动的状态,这里提供所有都能拖动的方法. procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseB ...
- sql 子查询要命名
Date1 from ( select distinct Date1 from TableName where Date1 > '2013-5-1' )A --这里加个A,B,C随便你 或者as ...
- STM32F0xx_PWR低功耗配置详细过程
Ⅰ.概述 今天总结PWR部分知识,请看“STM32F0x128参考手册V8”第六章.提供的软件工程是关于电源管理中的停机模式,工程比较常见,但也是比较简单的一个实例,根据项目的不同还需要适当修改或者添 ...