LeetCode OJ-- Sort List **@
链表排序,要求使用 O(nlgn) 时间,常量空间。
使用归并的思路
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *findMid(ListNode *head)
{
ListNode *mid;
ListNode *fast = head->next;
ListNode *slow = head;
while(fast && fast->next)
{
fast = fast->next;
fast = fast->next;
slow = slow->next;
}
mid = slow;
return mid;
}
ListNode *sortList(ListNode *head) {
if(head == NULL || head->next == NULL)
return head;
ListNode *tmp = findMid(head);
ListNode *right = sortList(tmp->next);
tmp->next = NULL;
ListNode *left = sortList(head); ListNode *newHead = new ListNode(-);
newHead->next = merge(left,right);
return newHead->next;
}
ListNode *merge(ListNode *left, ListNode *right)
{
ListNode *dummy = new ListNode(-);
for(ListNode *p = dummy; left!=NULL || right!= NULL; p = p->next)
{
int leftVal = left == NULL? INT_MAX:left->val;
int rightVal = right == NULL?INT_MAX:right->val;
if(leftVal <= rightVal)
{
p->next = left;
left = left->next;
}
else
{
p->next = right;
right = right->next;
}
}
return dummy->next;
}
};
LeetCode OJ-- Sort List **@的更多相关文章
- [LeetCode OJ] Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- 待字闺中之快排单向链表;leetcode之Sort List
题目来源.待字闺中.原创@陈利人 .欢迎大家继续关注微信公众账号"待字闺中" 分析:思路和数据的高速排序一样,都须要找到一个pivot元素.或者节点. 然后将数组或者单向链表划分为 ...
随机推荐
- springboot教程
http://www.cnblogs.com/java-zhao/tag/spring-boot/ http://blog.csdn.net/liaokailin/article/category/5 ...
- click事件
click事件是可以多次绑定的,如果绑定多次就会执行多次,因此再不需要重复执行的情况下,就需要使用unbind对事件进行解绑
- 关于javascript tween的学后小感想
今天决定了解一下tween算法,首先得下载个tween.js看看吧,好吧,有点被惊艳到了. 也让我想起了之前上数学课时,听到过的一句话:“数学世界是神秘.纯洁.有魅力的”,一直 记得这句话,期待有朝一 ...
- ios 使用可视化工具charles转换pcap文件,进行流量统计(通过tcpdump抓包)
环境准备:使用mac电脑,下载xcode,Charles 连接iPhone手机,打开xcode-window-devices-查看设备UDID 打开终端:rvictl –s 设备号 ,查看虚拟端口号 ...
- 创建struct类型的数组
在autoit中,如何创建类似这样的数组呢?如下方式,数组的element只是存储的地址相邻,所以我们可以这样做 $tagMYSTRUCT = "int code; char msg[10] ...
- js实现基础运动
首先我定义3个div每个div当我鼠标放上去的时候,他的宽度就表达,如下图 首先是样式和html代码 <style> div{ width:100px; height:50px; back ...
- AIDL学习
(转自)可以参见:http://www.2cto.com/kf/201406/312244.html 1.为什么要有AIDL? 无论学什么东西,最先得弄明白为什么要有这个东西,不要说存在即是合理,存在 ...
- 一个CString的实现 拷贝构造函数的应用
class CString { public: CString (char* s); CString(); ~CString(); private: char *str; int len; stati ...
- vsftpd 修改默认目录
默认配置下: 匿名用户登录 vsftpd 服务后的根目录是 /var/ftp/:系统用户登录 vsftpd 服务后的根目录是系统用户的家目录. 若要修改登录 vsftpd 服务后的根目录,只要修改 / ...
- Selenium+Python+Eclipse网页自动化集成环境配置(附简单的测试程序)
最近公司在给我们培训,主要是网页自动化测试的,现在的工作每天都是测APP,刚刚入门,不过,当我看了别人写的bug之后,就觉得不会觉得能够发现bug多么多么的厉害了. 前两周的时间一直在搭建自动化测试的 ...