在使用O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。

详见:https://leetcode.com/problems/sort-list/description/

Java实现:

链表上的归并排序:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode slow=head;
ListNode fast=head;
ListNode mid=null;
while(fast!=null&&fast.next!=null){
mid=slow;
slow=slow.next;
fast=fast.next.next;
}
mid.next=null;
return mergeSort(sortList(head),sortList(slow));
}
private ListNode mergeSort(ListNode low,ListNode high){
ListNode helper=new ListNode(-1);
ListNode cur=helper;
while(low!=null&&high!=null){
if(low.val<high.val){
cur.next=low;
low=low.next;
}else{
cur.next=high;
high=high.next;
}
cur=cur.next;
}
cur.next=low!=null?low:high;
return helper.next;
}
}

链表上的快速排序:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
if(head==null||head.next==null){
return head;
}
quickSort(head,null);
return head;
}
private void quickSort(ListNode begin,ListNode end){
if(begin!=end){
ListNode seq=getSeperator(begin,end);
quickSort(begin,seq);
quickSort(seq.next,end);
}
}
private ListNode getSeperator(ListNode begin,ListNode end){
ListNode first=begin;
ListNode second=begin.next;
int pivot=first.val;
while(second!=end){
if(second.val<pivot){
first=first.next;
swap(first,second);
}
second=second.next;
}
swap(first,begin);
return first;
}
private void swap(ListNode a,ListNode b){
int tmp=a.val;
a.val=b.val;
b.val=tmp;
}
}

148 Sort List 链表上的归并排序和快速排序的更多相关文章

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

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

  2. [LeetCode]148. Sort List链表归并排序

    要求时间复杂度O(nlogn),空间复杂度O(1),采用归并排序 传统的归并排序空间复杂度是O(n),原因是要用一个数组表示合并后的数组,但是这里用链表表示有序链表合并后的链表,由于链表空间复杂度是O ...

  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. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

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

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

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

  6. Insertion Sort List——链表的插入排序

    Sort a linked list using insertion sort. 这道题跟 Sort List 类似,要求在链表上实现一种排序算法,这道题是指定实现插入排序.插入排序是一种O(n^2) ...

  7. 148. Sort List - LeetCode

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

  8. [LintCode] Sort List 链表排序

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

  9. Java for LeetCode 148 Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...

随机推荐

  1. 2017ACM/ICPC广西邀请赛 K- Query on A Tree trie树合并

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  2. springCloud和docker笔记(1)——微服务架构概述

    1.微服务设计原则 1)单一职责原则:只关注整个系统中单独.有界限的一部分(SOLID原则之一) 2)服务自治原则:具备独立的业务能力和运行环境,可独立开发.测试.构建.部署 3)轻量级通信机制:体量 ...

  3. Messaging Patterns for Event-Driven Microservices

    Messaging Patterns for Event-Driven Microservices https://content.pivotal.io/blog/messaging-patterns ...

  4. java -jar 与nohup的区别

    ——作为java程序员,经常会遇到这样一个问题,打个jar包,测试或者上线生产,于是乎面临的选择来了,java –jar or nohup? 下面我来扒一扒: 一.    java -jar a.ja ...

  5. SVN命令使用详解【转】

    本文转载自:http://blog.sina.com.cn/s/blog_963453200101eiuq.html 1.检出svn  co  http://路径(目录或文件的全路径) [本地目录全路 ...

  6. nginx、mysql、php等各编译参数查询

    查看nginx编译参数:/usr/local/nginx/sbin/nginx -V 查看apache编译参数:cat /usr/local/apache2/build/config.nice 查看m ...

  7. 类的加载、时机、反射、模板设计、jdk7/jdk8新特性(二十六)

    1.类的加载概述和加载时机 * A:类的加载概述 * 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. * 加载 * 就是指将class文 ...

  8. 根据用户时区显示当地时间 javascript+php

    在跨时区应用中会用到下面代码,这是以前写的一段代码. 服务器保存相关时间配置,保存形式为GMT时间,客户端需要根据客户所在时区做相应显示,以符合客户习惯. ​1. [代码][JavaScript]代码 ...

  9. 接口_简单get接口_第一个接口

    import flask,json # print(__name__) ##__name__代表当前这个python文件 server = flask.Flask(__name__) #把咱们当前的这 ...

  10. 书写优雅的shell脚本(四) - kill命令的合理使用

    Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后台进程就须 ...