在使用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. VUE 之 路由 VueRouter

    1.VueRouter的安装 1.1.https://unpkg.com/vue-router/dist/vue-router.js下载安装. 1.2.<script src="./s ...

  2. HDU 6155 Subsequence Count 线段树维护矩阵

    Subsequence Count Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Oth ...

  3. BZOJ 1042: [HAOI2008]硬币购物 容斥+背包

    1042: [HAOI2008]硬币购物 Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请 ...

  4. CentOS笔记-其他杂记

    1.忘记密码时,可以用single模式来修改密码,为了安全,可以禁用single模式,参考网址如下 Centos服务器禁止单用户模式(single)来增强系统安全 2.远程登录:ssh root@xx ...

  5. vmware 自动挂起

    用VMware跑虚拟机,经常会出现客户操作系统自己挂起的现象,怀疑是主机自己休眠的设置.设置之后,无效. 后来才发现不是主机休眠设置,还是应该设置客户操作系统中的休眠设置. 在客户机,控制面板  电源 ...

  6. this that 时间戳转日期 小程序 列表 与 加载

    var gd = getApp().globalData; var imgUrlApp = gd.imgUrlApp; var localImgPath = gd.localImgPath; var ...

  7. mvn_action

    validate(验证): 验证项目正确,并且所有必要信息可用. compile(编译): 编译项目源码 test(测试): 使用合适的单元测试框架测试编译后的源码. package(打包): 源码编 ...

  8. Delphi如何实现多国语言

    Delphi里的多语言处理方法都一样, 都是通过资源DLL的形式进行加载处理. Delphi在加载form数据的时候会判断当前的系统语言,然后根据语言加载不同的资源dll, 来实现多国语言的功能. 下 ...

  9. lc.exe 已退出 代码为 -1

    地址:http://jingyan.baidu.com/article/91f5db1bd0ace31c7f05e321.html

  10. codeforcfes Codeforces Round #287 (Div. 2) B. Amr and Pins

    B. Amr and Pins time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...