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. js操作对象属性用点和用中括号有什么不同

    书读百遍其义自见 学习<JavaScript设计模式>一书时,学习工厂模式这一章节,发现了对象后使用中括号的情况,如下: var Factory=function(type,content ...

  2. golang指针函数

    func main() { a := models.SmsVerify{} a.Id = 100 fmt.Println(a.Id) // 100 test111(a) fmt.Println(a.I ...

  3. PyInstaller库的使用

    PyInstaller库的使用 PyInstaller库用于将已经写好的py程序,转换成可以跨平台的可执行文件 使用方式 发布主要借助cmd命令行来实现.在当前目录的powershell下,输入 py ...

  4. Python- 【python无法更新pip】提示python.exe: No module named pip

    用Anaconda安装的python 版本无法更新pip导致不能安装第三方库: 用Anaconda Prompt安装第三方库: python -m pip install --upgrade pip ...

  5. standard_key.kmp

    [KeyRemap]keyVersion=2B33554467=[eraseeof]S36=[bof]B33554466=[pagedn]S35=[eof]B33554465=[pageup]B10= ...

  6. Sass函数-数字函数-floor()函数

    floor() 函数刚好与 ceil() 函数功能相反,其主要将一个数去除其小数部分,并且不做任何的进位.也就是只做舍,不做入的计算: >> floor(2.1) 2 >> f ...

  7. HTML基础 结构,标题<h1>和段落<p> 写一个三毛语录

    先看代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  8. Git初始化本地仓库及管理远程仓库github

    1.首先在本地安装git,地址:https://git-scm.com/downloads.下载安装好git工具. 2.将自己在github上的注册的用户名和邮箱写入本地git的配置文件中: (1). ...

  9. maven clean后 编译报错

    <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <config ...

  10. 商城分类导航实现 (css)

    代码实例:demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...