Sort a linked list in O(n log n) time using constant space complexity.

归并排序

struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; ListNode *sortList(ListNode *head) {
if (head==NULL || head->next == NULL){
return head;
}
//find the middle place
ListNode *p1=head, *p2=head->next; while(p2 && p2->next){
p1 = p1->next;
p2 = p2->next->next;
}
p2 = p1->next;
p1->next = NULL;
return mergeTwoLists(sortList(head), sortList(p2));
} ListNode *mergeTwoLists(ListNode* head1, ListNode* head2){
ListNode *p1 = head1, *p2=head2;
static ListNode dummy(); ListNode *tail = &dummy; while(p1 && p2){
if(p1->val < p2->val){
tail->next = p1;
p1 = p1->next;
}else{
tail->next = p2;
p2 = p2->next;
}
tail = tail->next;
}
if (p1) tail->next = p1;
if (p2) tail->next = p2; return dummy.next;
}

148. Sort List -- 时间复杂度O(n log n)的更多相关文章

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

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

  2. [leetcode sort]148. Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 以时间复杂度O(n log n)排序一个链表. 归并排序, ...

  3. 148. Sort List - LeetCode

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

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

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

  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. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...

  7. 【leetcode】148. Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 链表排序可以用很多方法,插入,冒泡,选择都可以,也容易实现 ...

  8. 148. Sort List (java 给单链表排序)

    题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...

  9. Leetcode之148. Sort List Medium

    https://leetcode.com/problems/sort-list/ Sort a linked list in O(n log n) time using constant space ...

随机推荐

  1. 3.29考试(HNOI难度)

    一. 城镇 [ town ]   Memory Limit: 128 MB    Time Limit : 1s Description 在 farmer land 上,有 N 个 farmer to ...

  2. window.location.hash

    我们知道JavaScript中很早就提供了window.history对象,利用history对象的forward().go().back()方法能够方便实现不同页面之间的前进.后退等这种导航功能.但 ...

  3. Java中的内省

    为什么要学内省? •开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作java对象的属性.   什么是Ja ...

  4. 用CSS样式截取字符串,多的用省略号表示

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  5. PPPOE协议

    PPPOE协议 PPPOE的全称为PPP Over Ethernet,用于实现PPP在以太网上的传输.它要求通信双方是点到点的关系,不适于广播型的以太网和一些多点访问型网络. 一.PPPOE的作用 将 ...

  6. Android入门:绑定本地服务

    一.绑定服务介绍   前面文章中讲过一般的通过startService开启的服务,当访问者关闭时,服务仍然存在: 但是如果存在这样一种情况:访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定: ...

  7. Maven——使用Maven构建多模块项目

    原文:http://www.cnblogs.com/xdp-gacl/p/4242221.html 在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为doma ...

  8. Linux runlevel 运行级别

    runlevel可以认为是系统状态,形象一点,您可以认为runlevel有点象微软的windows操作系统中的Normal,safemode,和Command prompt only. Linux系统 ...

  9. sql语句语法大全

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...

  10. aspx后缀映射成html

    1.网站的配置文件添加如下代码: <configuration> <configSections> <section name="RewriterConfig& ...