原题地址

链表归并排序

真是恶心的一道题啊,哇了好多次才过。

代码:

 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的更多相关文章

  1. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

  2. [LeetCode] 148. Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...

  3. [LeetCode] 148. Sort List 解题思路

    Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...

  4. Java for LeetCode 148 Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...

  5. leetcode 148. Sort List ----- java

    Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...

  6. LeetCode 148 Sort List 链表上的归并排序和快速排序

    Sort a linked list in O(n log n) time using constant space complexity. 单链表排序----快排 & 归并排序 (1)归并排 ...

  7. [LeetCode]148. Sort List链表归并排序

    要求时间复杂度O(nlogn),空间复杂度O(1),采用归并排序 传统的归并排序空间复杂度是O(n),原因是要用一个数组表示合并后的数组,但是这里用链表表示有序链表合并后的链表,由于链表空间复杂度是O ...

  8. 148. Sort List - LeetCode

    Solution 148. Sort List Question 题目大意:对链表进行排序 思路:链表转为数组,数组用二分法排序 Java实现: public ListNode sortList(Li ...

  9. 待字闺中之快排单向链表;leetcode之Sort List

    题目来源.待字闺中.原创@陈利人 .欢迎大家继续关注微信公众账号"待字闺中" 分析:思路和数据的高速排序一样,都须要找到一个pivot元素.或者节点. 然后将数组或者单向链表划分为 ...

随机推荐

  1. MySQL: ON DUPLICATE KEY UPDATE 用法 避免重复插入数据

    INSERT INTO osc_visit_stats(stat_date,type,id,view_count) VALUES (?,?,?,?) ON DUPLICATEKEY UPDATE vi ...

  2. phpcms v9 源码解析-1 index.php

    这个是phpcms V9 的入口文件index.php. V9程序的执行绝大多数是从这个文件开始的,但不绝对,在项目下面的api.php和plugin.php是另外的入口文件,这里我们先不做深究. 在 ...

  3. Eclipse之Failed to load the JNI shared library”……\jvm.dll”的解决方案

    问题描述:java环境变量配置完全正确,但是运行eclipse时提示:Failed to load the JNI shared library " xxx\jva.dll" 原因 ...

  4. delphi函数调用约定

    指令 参数存放位置 参数传递顺序 参数内存管理 使用地方 Register CPU寄存器 从左到右 被调用者 默认,published属性存取方法必须使用 Pascal 栈 从左到右 被调用者 向后兼 ...

  5. 相比于汇编语言的准确性c语言延时精确度如何提升

    只要合理的运用,C还是可以达到意想不到的效果.很多朋友抱怨C效率比汇编差了很多,其实如果对Keil C的编译原理有一个较深入的理解,是可以通过恰当的语法运用,让生成的C代码达到最优化.即使这看起来不大 ...

  6. 6.css文本样式

    文本样式,只要针对的是文本的效果和文本的方位,即文本样式和文本控制总结起来有一表中的属性可用: 属性名 说明 CSS 版本 text-decoration 装饰文本出现各种划线 1 text-tran ...

  7. [转]C++编写Config类读取配置文件

    //Config.h #pragma once #include <string> #include <map> #include <iostream> #incl ...

  8. oracle11g 新特性 - rman自动备份控制文件延迟

    OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 -6 ...

  9. Android HTTP session && cookie

    HTTP协议与状态保持HTTP协议本身是无状态的,这与HTTP协议本来的目的是相符的,客户端只需要简单的向服务器请求下载某些文件,无论是客户端还是服务器都没有必要纪录彼此过去的行为,每一次请求之间都是 ...

  10. 常见css的兼容问题

    链接的虚线框问题 <!-- html --> <a class="noDashedBox" href="#"><img src=& ...