【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 ...
随机推荐
- java操作xml
package com.xml.zh; import javax.xml.parsers.*; import org.w3c.dom.*; public class XmlTest1{ /** * 使 ...
- 【poj3714】 Raid
http://poj.org/problem?id=3714 (题目链接) 现在才搞平面最近点对..感觉有点尴尬 题意 给出平面上两组点,每组n个,求两组点之间最短距离 Solution1 平面最近点 ...
- BZOJ2115 [Wc2011] Xor
Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...
- Threat Risk Modeling Learning
相关学习资料 http://msdn.microsoft.com/en-us/library/aa302419(d=printer).aspx http://msdn.microsoft.com/li ...
- NSKeyedArchiver 类 格式
结论:NSKeyedArchiver 以健值对+类名的形式保存数据: NSDictionary *tDic = @{@"kxxxx":@"hhhh",@&quo ...
- MyEclipse中防止代码格式化时出现换行的情况的设置
编辑完成代码,用MyEclipse的代码格式化后,本来不长的代码也被自动转成了多行.虽然自动换行以后在编辑器中一眼就能看到全部的代码,但是可读性却大打折扣,避免出现这种情况的办法是: 1.Java代码 ...
- SQL Server 2005 数据库复制(转载)
对于一个地域分散的大型企业组织来说,构建具有典型的分布式计算机特征的大型企业管理信息系统时,总要解决一个很重要的问题:如何在多个不同数 据库服务器之间保证共享数据的一致性.之所以有这个重要的问题在于企 ...
- mybatis中的resultMap
resultMap <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBL ...
- Java实现Socket5代理服务器
直接贴代码,不解释 1 主服务,用来侦听端口 package org.javaren.proxy; import java.net.ServerSocket; import java.net.Sock ...
- Java&.Net虚拟机精简(GreenJVM&GreenDotNet发布) .
精简JRE体积的小工具:http://blog.csdn.net/cping1982/archive/2008/09/02/2865198.aspx 项目地址:http://code.google.c ...