Sort a linked list in O(n log n) time using constant space complexity.
class Solution {
public:
ListNode* findMiddle(ListNode* head){
ListNode* chaser = head;
ListNode* runner = head->next;
while(runner != NULL && runner->next != NULL){
chaser = chaser->next;
runner = runner->next->next;
}
return chaser;
}
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == NULL){
return l2;
}
if(l2 == NULL){
return l1;
}
ListNode* dummy = new ListNode(0);
ListNode* head = dummy;
while(l1 != NULL && l2 != NULL){
if(l1->val > l2->val){
head->next = l2;
l2 = l2->next;
}
else{
head->next = l1;
l1 = l1->next;
}
head = head->next;
}
if(l1 == NULL){
head ->next = l2;
}
if(l2 == NULL){
head->next = l1;
}
return dummy->next;
}
ListNode* sortList(ListNode* head) {
if(head == NULL || head ->next == NULL){
return head;
}
ListNode* middle = findMiddle(head);
ListNode* right = sortList(middle->next);
middle -> next = NULL;
ListNode* left = sortList(head);
return mergeTwoLists(left, right);
}
};
Sort a linked list in O(n log n) time using constant space complexity.的更多相关文章
- Data Structure Linked List: Merge Sort for Linked Lists
http://www.geeksforgeeks.org/merge-sort-for-linked-list/ #include <iostream> #include <vect ...
- 148. Sort List -- 时间复杂度O(n log n)
Sort a linked list in O(n log n) time using constant space complexity. 归并排序 struct ListNode { int va ...
- [Linked List]Sort List
otal Accepted: 59473 Total Submissions: 253637 Difficulty: Medium Sort a linked list in O(n log n) t ...
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
- [LintCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Have you met this question in ...
- leetcode sort List
Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-l ...
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
- 9. Sort List && Insertion Sort List (链表排序总结)
Sort List Sort a linked list in O(n log n) time using constant space complexity. H ...
- 【leetcode】Sort List
Sort List Sort a linked list in O(n log n) time using constant space complexity. 需要采用归并排序对链表进行操作. ...
随机推荐
- JMeter--PerfMon Metrics Collector监控内存及CPU
1.需要准备的软件及插件 ServerAgent-2.2.1.zip JMeterPlugins-Standard-1.3.1.zip 2.jmeter上JMeterPlugins-Standard- ...
- 上白泽慧音——tarjian
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- C# 枚举类型的描述信息获取
新建一个控制台方法,写好自己的枚举类型: 如图: 在里面添加获取描述的方法: 具体源码: 链接:http://pan.baidu.com/s/1nv4rGkp 密码:byz8
- java错误笔记
错误1. Description Resource Path Location Type The superclass "javax.servlet.http.HttpSer ...
- 进程间通信,把字符串指针作为参数通过SendMessage传递给另一个进程,不起作用
参数发送进程: CString csCmd=AfxGetApp()->m_lpCmdLine; if (!csCmd.IsEmpty()) { pWndPrev->SendMessage( ...
- Ubuntu下HTTPS配置
Ubuntu下HTTPS配置非常简单,对大部分用户而言,使用普通的自签名证书,只需按照步骤进行就可以了,无需了解密钥.证书的更多知识,更深的背景知识还有RSA算法.DES算法.X509规范.CA机构. ...
- 对于HDMI设备连接状态的监听
对与最近主要做的是电视机盒子端的开发,其中涉及到设备的状态监听比较繁琐,所以对HDMI的连接状态的监听方法做个记录,方便后续查看. 主要通过两种方式: (1)比较常用的广播监听 注册一个动态广播来获取 ...
- COGS 1144. [尼伯龙根之歌] 精灵魔法
★ 输入文件:alfheim.in 输出文件:alfheim.out 简单对比时间限制:1 s 内存限制:128 MB [题目背景] 『谜题在丛林中散发芳香绿叶上露珠跳跃着歌唱火焰在隐 ...
- 洛谷 2543 [AHOI2004]奇怪的字符串
题目描述 输入输出格式 输入格式: 输入文件中包含两个字符串X和Y.当中两字符串非0即1.序列长度均小于9999. 输出格式: X和Y的最长公共子序列长度. 输入输出样例 输入样例#1: 010101 ...
- python代理检测
import socket,threading,os,sys,queue,re socket.setdefaulttimeout(5) path=sys.path[0] if os.path.isfi ...