Level:

  Medium

题目描述:

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

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

思路分析:

  对链表进行归并排序,时间复杂为O(nlgn),空间复杂度为O(1)。

代码:

public class Solution{
public ListNode sortList(ListNode head){
if(head==null||head.next=null)
return head;
ListNode slow=head;
ListNode fast=head;
while(fast.next!=null&&fast.next.next!=null){
slow=slow.next;
fast=fast.next.next;
}
ListNode right=slow.next;
slow.next=null;//将左右链表断开
return merge(sortList(head),sortList(right));
}
public ListNode merge(ListNode head,ListNode right){
if(head==null)
return right;
if(right==null)
return head;
ListNode pHead=new ListNode(0);
if(head.val<right.val){
pHead=head;
pHead.next=merge(head.next,right);
}else{
pHead=right;
pHead.next=merge(head,right.next);
}
return pHead;
}
}

45.Sort List(链表排序)的更多相关文章

  1. [LeetCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...

  2. LeetCode Sort List 链表排序(规定 O(nlogn) )

    Status: AcceptedRuntime: 66 ms 题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序.由时间复杂度想到快排.归并这两种排序.本次用的是归并排序.递归将链表的规模不 ...

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

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

  4. [LintCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Have you met this question in ...

  5. [LeetCode]147. Insertion Sort List链表排序

    插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...

  6. sort 树 hash 排序

    STL 中 sort 函数用法简介 做 ACM 题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的 O(n^2) 排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错. ST ...

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

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

  8. 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...

    链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...

  9. C语言 链表的使用(链表的增删查改,链表逆转,链表排序)

    //链表的使用 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include< ...

  10. c语言:链表排序, 链表反转

    下面将实现链表排序的排序和遍历显示功能: 所定义的链表结构如下: head -> p1 -> p2 ->p3 ->....->pn; head的本身不作为数据节点,hea ...

随机推荐

  1. HttpGet请求传递数组(集合)

    在HttpGet请求是传递数组(集合)的方法: 1.使用Ajax方法传递 eg: ajax.({ url:/test, data:["], type:"get" }); ...

  2. CSS样式表能否控制文字禁止选择,复制, 焦点

    div中禁止文字被选择 在做div的点击计数事件时,遇到一个小问题. 因为div里面有文字,所以当点击多次时,特别是鼠标点的比较快的时候,文字会被选中. 查了下,用css和javascript可以实现 ...

  3. elasticsearch 基础 —— Common Terms Query常用术语查询

    常用术语查询 该common术语查询是一个现代的替代提高了精确度和搜索结果的召回(采取禁用词进去),在不牺牲性能的禁用词. 问题 查询中的每个术语都有成本.搜索"The brown fox& ...

  4. 屏幕坐标点转UGUI坐标【包含屏幕适配】

    using UnityEngine; public class ScreenToUI : MonoBehaviour { public const float UI_Width = 1366f; pu ...

  5. fputc, fputs, putc, putchar, puts - 输出字符和字符串

    总览 (SYNOPSIS) #include <stdio.h> int fputc(int c, FILE *stream); int fputs(const char *s, FILE ...

  6. hadoop1.2.1配置与运行子串统计程序

    一.虚拟机版本 VirtualBox-4.3.30 二.操作系统 CentOS-6.7-x86_64-bin-DVD1.iso 下载地址1:http://www.centoscn.com/Centos ...

  7. Winfrom 弹出窗体位置设定

    Winfrom 窗体弹出位置设定,其实就是两种模式,第一种模式是通过Winform提供的属性来设定:第二种模式是自定义,可以相对于软件本身,也可以是相对于屏幕. 一.第一种模式 使用Winform提供 ...

  8. YOLOv1到YOLOv3的演变过程及每个算法详解

    1,YOLOv1算法的简介 YOLO算法使用深度神经网络进行对象的位置检测以及分类,主要的特点是速度够快,而且准确率也很高,采用直接预测目标对象的边界框的方法,将候选区和对象识别这两个阶段合二为一, ...

  9. oracle多表连接方式Hash Join Nested Loop Join Merge Join

    在查看sql执行计划时,我们会发现表的连接方式有多种,本文对表的连接方式进行介绍以便更好看懂执行计划和理解sql执行原理. 一.连接方式:        嵌套循环(Nested  Loops (NL) ...

  10. python在windows中运行文件

    "d:Program Files\python35\python.exe" hello.txt