【leetcode】Sort List
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public: ListNode *mergeList(ListNode *h1,ListNode *h2)
{
ListNode *head=new ListNode();
ListNode *cur=head;
while(h1!=NULL&&h2!=NULL)
{
if(h1->val>h2->val)
{
cur->next=h2;
h2=h2->next;
}
else
{
cur->next=h1;
h1=h1->next;
} cur=cur->next;
} cur->next=h1==NULL?h2:h1; cur=head;
head=head->next;
delete cur; return head;
} ListNode *sortList(ListNode *head) { if(head==NULL||head->next==NULL) return head; ListNode *slow,*fast;
slow=fast=head;
while(fast->next!=NULL&&fast->next->next!=NULL)
{
fast=fast->next->next;
slow=slow->next;
} ListNode *mid=slow->next;
slow->next=NULL; ListNode *h1=sortList(head);
ListNode *h2=sortList(mid);
return mergeList(h1,h2); }
};
【leetcode】Sort List的更多相关文章
- 【LeetCode】Sort Colors 解题报告
[题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Leetcode】Sort List JAVA实现
Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...
- 【LeetCode】 sort list 单清单归并
称号:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排 ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【leetcode】Sort Colors(middle)☆
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
- 【Leetcode】Sort List (Sorting)
这个问题需要与归并排序排两个名单,基本思路分为切割与合并 合并后的代码Merge Two Sorted List里已经讲得非常清楚了. 所以这里直接给出代码. public ListNode merg ...
- 【leetcode】sort list(python)
链表的归并排序 超时的代码 class Solution: def merge(self, head1, head2): if head1 == None: return head2 if head2 ...
随机推荐
- ansible-1 的安装
该文章摘自:http://my.oschina.net/firxiao/blog/343395,该文章制作笔记使用,不做他用,转载请注明原文链接出处 Ansible 默认是基于SSH协议进行通信的. ...
- windows Server2008R2 每隔一段时间自动关机解决办法
情况描述: “我的电脑-->右键属性”中显示“已激活”,而“管理工具”中显示未激活.系统中有进程wlms.exe. 网上找了下解决方式: 1.提权工具:PSTOOLS(下载地址:http://m ...
- Java 工程师的学习线路图。
今天了一个超级好用的工具,思维导图 FreeMind,于是顺道试用了一下,照着画了一张 Java 工程师的学习线路图.
- VB中的属性、方法和事件概念解析
Visual Basic 语言中的所有对象都有它们自己的属性.方法和事件,其中包括窗体和控件.可以将属性视为对象的特性,将方法视为对象的操作,而将事件视为对象的响应. 日常生活中的对象(如氦气球)也具 ...
- Oracle常用sql
Oracle不像Sqlserver,并没有提供l默认约束,但提供了默认值,效果一样.--------------------------- 在建表时设置默认约束-------------------- ...
- Spring MVC实现防止表单重复提交(转)
Spring MVC拦截器+注解方式实现防止表单重复提交
- Uva11729 Commando War
相邻两个士兵交换顺序,不会对其他的有所影响,贪心考虑两两之间交换策略即可. sort大法好.印象中这类排序题里有一种会卡sort,只能冒泡排序,然而到现在还没有遇到 /**/ #include< ...
- DNS安全浅议、域名A记录(ANAME),MX记录,CNAME记录
相关学习资料 http://baike.baidu.com/link?url=77B3BYIuVsB3MpK1nOQXI-JbS-AP5MvREzSnnedU7F9_G8l_Kvbkt_O2gKqFw ...
- Alpha阶段个人总结
一. 工作总结 第一次接触通信和服务端框架,在前期准备阶段学习了很多不同的技术,也相应花费了很多时间去尝试.刚开始时还不知道框架这种东西,准备自己写,于是学习并尝试了一些简单的HTTP通信,比较麻烦, ...
- curl 命令行应用
我一向以为,curl只是一个编程用的函数库. 最近才发现,这个命令本身,就是一个无比有用的网站开发工具,请看我整理的它的用法. =================================== ...