题目:

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

思路:

nlogn的排序有快速排序、归并排序、堆排序。双向链表用快排比较适合,堆排序也可以用于链表,单向链表适合用归并排序。

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var sortList = function(head) {
if(head==null||head.next==null){
return head;
}else{
var slow=head,fast=head;
while(fast.next!=null&&fast.next.next!=null){
slow=slow.next;
fast=fast.next.next;
}
//拆成两个链表
fast=slow;
slow=slow.next;
fast.next=null; fast=sortList(head);
slow=sortList(slow);
return merge(fast,slow);
}
}; function merge(head1,head2){
if(head1==null){
return head2;
}
if(head2==null){
return head1;
}
var res=new ListNode(),p=new ListNode();
if(head1.val<head2.val){
res=head1;
head1=head1.next;
}else{
res=head2;
head2=head2.next;
}
p=res; while(head1!=null&&head2!=null){
if(head1.val<head2.val){
p.next=head1;
head1=head1.next;
}else{
p.next=head2;
head2=head2.next;
}
p=p.next;
} if(head1!=null){
p.next=head1;
}else if(head2!=null){
p.next=head2;
} return res;
}

【链表】Sort List(归并排序)的更多相关文章

  1. Sort List——经典(链表中的归并排序)

    Sort a linked list in O(n log n) time using constant space complexity.    对一个链表进行排序,且时间复杂度要求为 O(n lo ...

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

    在使用O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 详见:https://leetcode.com/problems/sort-list/description/ Java实 ...

  3. 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)

    连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...

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

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

  5. [SOJ]Easy sort (归并排序)

    Description You know sorting is very important. And this easy problem is: Given you an array with N ...

  6. [Swift]LeetCode148. 排序链表 | Sort List

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

  7. Natural Merge Sort(自然归并排序)

    This is a Natural Merge Sort program from my textbook. It works, but I don't think it's good. // Nat ...

  8. js实现冒泡排序(bubble sort)快速排序(quick sort)归并排序(merge sort)

    排序问题相信大家都比较熟悉了.用js简单写了一下几种常用的排序实现.其中使用了es6的一些语法,并且不仅限于数字--支持各种类型的数据的排序.那么直接上代码: function compare (a, ...

  9. Sort List 典型链表

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

随机推荐

  1. JavaNIO学习一

    文章来源:http://cucaracha.iteye.com/blog/2041847 对文件来说,可能最常用的操作就是创建.读取和写出.NIO.2 提供了丰富的方法来完成这些任务.本文从简单的小文 ...

  2. changetoutf-8

    import chardet import os # ANSI文件转UTF-8 import codecs import os def strJudgeCode(str): return charde ...

  3. 信息管理代码分析<二>读取二进制文件数据

    first和end做为全局变量,分别指向链表的头和尾.建立链表的方式也比较简易,从二进制文件数据块中,依次从头到尾读取,每读取一个就建立一个结点. /*基本模型*/ EMP *emp1; while( ...

  4. hdu 4888 最大流慢板

    http://acm.hdu.edu.cn/showproblem.php?pid=4888 添加一个源点与汇点,建图如下: 1. 源点 -> 每一行对应的点,流量限制为该行的和 2. 每一行对 ...

  5. MFC-CWinApp

    概览 CWinApp为应用程序类, CWinApp由 CWinThread 派生而来 ,后者表示可能具有一个或多个线程的应用程序的主执行线程,基于框架生成的应用程序必须有且仅有一个从 CWinApp ...

  6. Delphi Dll 动态调用例子(1)

    http://blog.sina.com.cn/s/blog_62c46c3701010q7h.html 一.编写dll library TestDllByD2007; uses  SysUtils, ...

  7. redis 任务队列

    使用Redis实现任务队列 说到队列很自然就能想到Redis的列表类型,3.4.2节介绍了使用LPUSH和RPOP命令实现队列的概念.如果要实现任务队列,只需要让生产者将任务使用LPUSH命令加入到某 ...

  8. express session

    一.什么是session? 最近在学习node.js 的express框架,接触到了关于session方面的内容.翻阅了一些的博客,学到了不少东西,发现一篇博文讲的很好,概念内容摘抄如下: Sessi ...

  9. Spring Boot 2 实践记录之 组合注解原理

    Spring 的组合注解功能,网上有很多文章介绍,不过都是介绍其使用方法,鲜有其原理解析. 组合注解并非 Java 的原生能力.就是说,想通过用「注解A」来注解「注解B」,再用「注解B」 来注解 C( ...

  10. 经典串匹配算法(KMP)解析

    一.问题重述 现有字符串S1,求S1中与字符串S2完全匹配的部分,例如: S1 = "ababaababc" S2 = "ababc" 那么得到匹配的结果是5( ...