【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 ...
随机推荐
- UVA5876 Writings on the Wall 扩展KMP
扩展KMP的简单题. #include<stdio.h> #include<string.h> #define maxn 51010 char s[maxn],t[maxn]; ...
- druid(德鲁伊)数据源的使用和配置 阿里出品
pom.xml <dependency> <groupId>com.alibaba</groupId> <artifactId>drui ...
- 【USACO1.1】Broken Necklace
题意 一个环形项链,有rbw三种珠子,r代表red,b代表blue,w代表white,从任意一个位置断开,两端分别取珠子,同一端取的珠子要相同颜色,w可以染成想要的颜色,即既可当作r也可以当作b,求最 ...
- 用php生成数据字典
<?php header("Content-type: text/html; charset=utf-8"); $dbserver = "localhost&quo ...
- shell 命令遇到的一些问题
1. command not found 一般都是未安装,需要root 权限去安装服务,就可正常使用.比如rz, sz, crontab, sendemail, lftp等 2. rz 传输失败,输 ...
- BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue
闻说HNOI每年都有一道Hash. 1206: [HNOI2005]虚拟内存 Time Limit: 50 Sec Memory Limit: 162 MB Submit: 330 Solved: 2 ...
- POJ3784 Running Median
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1670 Accepted: 823 Description For th ...
- HDU 2242 考研路茫茫----空调教室
传送门 考研路茫茫——空调教室 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Laravel5.1 启动详解
借鉴: Laravel所有请求的入口文件是:/public/index.php,代码如下 <?php /*|------------------------------------------- ...
- POJ3259Wormholes(判断是否存在负回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38300 Accepted: 14095 Descr ...