在使用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. setTimeout 传递的方法

    function waitExe(param){ if(time < 20){ time ++; $("#content").html(time); var self=thi ...

  2. javascript XMLHttpRequest 对象的open() 方法参数说明

    下文是从w3c上摘录下来的,其中参数 method 说明的很简短,不是很理解,所以又找了些资料作为补充.文中带括号部分. XMLHttpRequest.open() 初始化 HTTP 请求参数 语法o ...

  3. Javascript版五子棋

    Javascript版五子棋,无禁手.欢迎提出算法的改进意见.2. [代码]HTML     <!DOCTYPE html><html>    <head>    ...

  4. fuse的mount机制 2 -系统调用mount

    经过上一篇的分析,目前已经知道mount函数最终进入到mount.c 中的 int fuse_kern_mount(const char *mountpoint, struct fuse_args * ...

  5. 嵌入式Linux学习方法——给那些彷徨者(上)

    要想学好嵌入式Linux,首先要解决两个重要问题: 1. 学什么? 2. 怎么学? 首先解决第一个问题. 嵌入式Linux的系统架构包括软件和硬件两个部分,如下图: 再来看看一个成熟的嵌入式产品的开发 ...

  6. sharepoint服务器修改密码后出现HTTP Error 503

    HTTP Error 503   解决办法: 更改sharepoint 网站应用程序池标示后,更改标示重新输入管理员密码,问题解决!

  7. E - Alice and Bob

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. 1、HTML的本质以及在web中的作用

    一.HTML 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: -写Html文件(充当模板的作用)****** -数据库获取数据,然后替换到html文件的指定位置(We ...

  9. python 模块和包的使用方法

    一.模块 1.import导入模块 import module1,mudule2... 2.from...import...导入模块 导入指定内容 from modname import name1[ ...

  10. 11.6NOIP模拟赛

    [数据规模和限制] 对于全部测试数据,满足 N,M,K≤,W≤ 各个测试点的数据规模及特殊性质如下表. 测试点 N M K ≤ ≤ ≤ ≤ ≤ ≤ ≤ ≤ ≤ ≤ 师 更多咨询:北京信息学窦老师 QQ ...