Leetcode#148 Sort List
链表归并排序
真是恶心的一道题啊,哇了好多次才过。
代码:
void mergeList(ListNode *a, ListNode *b, ListNode *&h, ListNode *&t) {
h = t = NULL;
while (a && b) {
ListNode *c = NULL;
if (a->val <= b->val) {
c = a;
a = a->next;
}
else {
c = b;
b = b->next;
}
if (!h)
h = t = c;
else {
t->next = c;
t = t->next;
}
}
while (a) {
t->next = a;
t = t->next;
a = a->next;
}
while (b) {
t->next = b;
t = t->next;
b = b->next;
}
}
ListNode *sortList(ListNode *head) {
ListNode *prev = NULL;
ListNode *h1 = NULL;
ListNode *h2 = NULL;
ListNode *t1 = NULL;
ListNode *t2 = NULL;
ListNode *node = NULL;
int len = ;
node = head;
while (node && (++len))
node = node->next;
for (int l = ; l < len; l <<= ) {
prev = NULL;
h1 = NULL;
h2 = NULL;
t1 = NULL;
t2 = NULL;
node = head;
while (node) {
h1 = t1 = node;
for (int i = ; node && i < l; i++) {
t1 = node;
node = node->next;
}
if (t1)
t1->next = NULL;
h2 = t2 = node;
for (int i = ; node && i < l; i++) {
t2 = node;
node = node->next;
}
if (t2)
t2->next = NULL;
ListNode *h, *t;
if (h2)
mergeList(h1, h2, h, t);
else {
h = h1;
t = t1;
}
if (!prev)
head = h;
else
prev->next = h;
t->next = node;
prev = t;
}
}
return head;
}
Leetcode#148 Sort List的更多相关文章
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- [LeetCode] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- [LeetCode] 148. Sort List 解题思路
Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...
- Java for LeetCode 148 Sort List
Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...
- leetcode 148. Sort List ----- java
Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...
- LeetCode 148 Sort List 链表上的归并排序和快速排序
Sort a linked list in O(n log n) time using constant space complexity. 单链表排序----快排 & 归并排序 (1)归并排 ...
- [LeetCode]148. Sort List链表归并排序
要求时间复杂度O(nlogn),空间复杂度O(1),采用归并排序 传统的归并排序空间复杂度是O(n),原因是要用一个数组表示合并后的数组,但是这里用链表表示有序链表合并后的链表,由于链表空间复杂度是O ...
- 148. Sort List - LeetCode
Solution 148. Sort List Question 题目大意:对链表进行排序 思路:链表转为数组,数组用二分法排序 Java实现: public ListNode sortList(Li ...
- 待字闺中之快排单向链表;leetcode之Sort List
题目来源.待字闺中.原创@陈利人 .欢迎大家继续关注微信公众账号"待字闺中" 分析:思路和数据的高速排序一样,都须要找到一个pivot元素.或者节点. 然后将数组或者单向链表划分为 ...
随机推荐
- asp.net解析请求报文
NameValueCollection myHeader = new NameValueCollection(); int i; string strKey; string result; myHea ...
- SQL Server 一些关键字详解(二)
1.LEFT JOIN 容易让人误解的地方 背景:因为在网上搜了下 LEFT JOIN 和 OUTER APPLY 的区别,时发现,有的网友解释为: 1) A left join B 的连接 ...
- JavaWeb之 JSP:内置对象,EL表达式,JSP标签基础
JSP的内置对象 什么是JSP的内置对象呢? 在JSP页面进行编程的时候,如果我们要使用一些对象,如:HttpSession,ServletConfig,ServletContext这些对象,如果每次 ...
- 算法系列1《DES》
1. DES算法简介 DES算法全称为Data Encryption Standard,即数据加密算法,它是IBM公司于1975年研究成功并公开发表的.DES算法的入口参数有三个:Key.Data.M ...
- Linux下安装宋体以及微软雅黑字体
最近工作用itext生成pdf在windows环境下没有出现中文乱码而在linux下出现中文乱码,打开pdf查看pdf编码,以及显示的编码,发现编码并没有对应.原因是使用的宋体和微软雅黑在linux环 ...
- 解决mysql登陆时出现“ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql/mysql.sock' (2)”
mariadb同样适用 首先检查mysql状态 linux-6yo1:~ # /etc/init.d/mysql status Checking for service MySQL: unused m ...
- golang的哪些坑爷事: package实践
在golang中package是个困惑的概念, 特别是package还可以与folder不同名, 委实让我恶心了一把. 关于golang的package的最佳实践: package is folder ...
- jira插件带ui界面和几种方式
http://localhost:2990/jira/plugins/servlet/issuecrud jira插件带ui界面和几种方式 https://developer.atlassian.co ...
- UEFI双硬盘安装win8.1和Ubuntu14.04
UEFI双硬盘安装win8.1和Ubuntu14.04 安装环境 UEFI启动模式 双GPT硬盘 一个ssd 一个hdd 笔记本已安装win8.1 硬盘启动顺序为: U盘 ssd hdd 光驱 安装方 ...
- angularjs resources
http://tutorials.jenkov.com/angularjs/watch-digest-apply.html http://angular-tips.com/blog/2013/08/w ...