题目:

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. AE IRasterCursor 获取栅格图层像素值

    在编写使用栅格图层的代码时,常常要获取栅格图层的像素值(PixelValue).如果想获取某一点的像素值,可以使用IRaster2中的getPixelValue方法.但如果想要获得的是图层中的某一块甚 ...

  2. 简单的nginx模拟网站的负载均衡

    环境:nginx1.10.3 虚拟机环境:3台centos7虚拟机 将148机器作为转发服务器配置如下 监听80端口,在http里面配置如下 将edc.com分别转发到149和150的服务器上 本地主 ...

  3. php一些方法说明

    var_dump():判断一个变量的类型与长度,并输出变量的数值,如果变量有值输的是变量的值并回返数据类型.此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值.数组将递归展开值,通过缩进显 ...

  4. 系统目录APK更新——权限问题

    package com.example.wx; import java.io.File;import java.io.FileOutputStream;import java.io.IOExcepti ...

  5. spring aop方式配置事务中的三个概念 pointcut advice advisor

    AOP的3个关键概念 因为AOP的概念难于理解,所以在前面首先对Java动态代理机制进行了一下讲解,从而使读者能够循序渐进地来理解AOP的思想. 学习AOP,关键在于理解AOP的思想,能够使用AOP. ...

  6. 通过oracle闪回查看表中值的变更履历信息

    http://www.oracle.com/technetwork/cn/articles/week1-10gdba-093837-zhs.html 得到电影而不是图片:闪回版本查询 不需要设置,立即 ...

  7. .NET高级代码审计(第二课) Json.Net反序列化漏洞

    0X00 前言 Newtonsoft.Json,这是一个开源的Json.Net库,官方地址:https://www.newtonsoft.com/json ,一个读写Json效率非常高的.Net库,在 ...

  8. Layui 手册一

    icon-图标 1:√2:×3:问号4:锁5:哭脸6:笑脸7:感叹号 使用layer.msg('',{ icon:1 });  目前只提供7种图标可选,用的适当还是很好看的. 表格刷新 parent. ...

  9. 微软官方实例 RazorPagesMovie 在 asp.net core 2.1 版本下的实战

    微软官方实例 RazorPagesMovie 在 asp.net core 2.1 版本下的实战 友情提示: 操作系统: MacOS 10.13.5 dotnet core: version 2.1. ...

  10. SpingBoot的认识和基本使用

    认识SpingBoot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程. -使用springboot以后,搭建一个spring应 ...