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

链表排序,要求时间复杂度O(nlgn),我写的归并排序。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode sortList(ListNode head) {
if(head==null){
return head;
}
ListNode tail = head;
while(tail.next!=null){
tail=tail.next;
}
return mergeList(head,tail);
} ListNode mergeList(ListNode head,ListNode tail){
if(head==tail){
return head;
}
ListNode mid = getMid(head,tail);
ListNode postList = mergeList(mid.next,tail);
mid.next=null;
ListNode preList = mergeList(head,mid);
return merge(preList,postList);
} ListNode getMid(ListNode head,ListNode tail){
if(head==tail){
return head;
}
ListNode fast = head,slow = head;
while(fast.next!=null&&fast.next.next!=null&&fast.next!=tail){
fast=fast.next.next;
slow=slow.next;
}
return slow;
}
ListNode merge(ListNode l1,ListNode l2){
if(l1==null||l2==null){
return l1==null?l2:l1;
}
ListNode ptr = new ListNode(0);
ListNode head = ptr;
while(l1!=null&&l2!=null){
if(l1.val<=l2.val){
ptr.next = l1;
l1=l1.next;
}else{
ptr.next = l2;
l2=l2.next;
}
ptr=ptr.next;
}
ptr.next=l1==null?l2:l1;
return head.next;
}
}

Sort List ——LeetCode的更多相关文章

  1. Sort List leetcode

    这个题一开始本想用快速排序的,但是想了20分钟都没有头绪,难点在于快速排序的随机访问无法用链表实现,不过如果可以实现快速排序partition函数就可以了,但是这可能比较复杂,于是改用其他排序方法,上 ...

  2. sort学习 - LeetCode #406 Queue Reconstruction by Height

    用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...

  3. Sort Colors [LeetCode]

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  4. Sort Colors —— LeetCode

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  5. Insertion Sort List —— LeetCode

    Sort a linked list using insertion sort. 题目大意:将一个单链表使用插入排序的方式排序. 解题思路:先新建一个头指针,然后重新构建一下这个单链表,每次从头找到第 ...

  6. sort vector - leetcode 新用法

    179. Largest Number sort(num.begin(), num.end(), [](int a, int b){ return to_string(a)+to_string(b) ...

  7. Insertion Sort List Leetcode

    Sort a linked list using insertion sort. 这个题我巧妙的设置了一个临时头结点 class Solution { public: ListNode* insert ...

  8. Sort Colors leetcode java

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  9. Sort List leetcode java

    题目: Sort a linked list in O(n log n) time using constant space complexity. 题解: 考虑到要求用O(nlogn)的时间复杂度和 ...

随机推荐

  1. Fileupload控件导致500错误

    问题: 今天遇到一个问题,用Fileupload控件上传Excel文件,用一个button控件调用“FileUpload1.SaveAs”方法,点击按钮后出现服务器500错误.如下图: 解决方法: 在 ...

  2. win2008 64位下.net 无法访问oracle

    这两天换了台新机子,就想弄个新系统win2008 64bit来测试下,也尝尝新鲜,结果是碰的头破血流啊,哈哈就像挖宝似的 环境:win2008 64bit + IIS7+.net2.0 +ORACLE ...

  3. C# 各种集合

    大多数集合都在  System.Collections,System.Collections.Generic两个命名空间. 其中System.Collections.Generic专门用于泛型集合. ...

  4. asp.net对word文档进行修改 对于使用word文档做模板编辑比较适用

    最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...

  5. SQL觸發器聯級刪除

    Create TRIGGER [dbo].[trigInstructionsDelete] ON dbo.Instructions instead OF DELETE AS BEGIN DECLARE ...

  6. JS计算指定日期是距今的第几周,星期几

    无意中在百度知道上发现这样一个问题,就抽时间见写了一个函数. 首先我们需要明确,既然是指定日期距今的第几周,那么就要知道指定的日期是什么,而且是不能确定的,会根据使用者不同而得到不同的日期,所以我们需 ...

  7. SGU 231.Prime Sum

    题意: 求有多少对质数(a,b)满足a<=b 且a+b也为质数.(a+b<=10^6) Solution: 除了2之外的质数都是奇数,两个奇数的和是偶数,不可能是质数.所以题目就是求差为2 ...

  8. Ueditor之SAE移植

    新浪SAE环境下使用UEditor http://www.cnblogs.com/zjzhome/p/3815460.html?utm_source=tuicool 在SAE上使用Ueditor的图片 ...

  9. TatukGIS-TGIS_ShapeArc.GetPointOnLine

    function GetPointOnLine(const _distance: Double; const _offset: Double; const _part: Integer): TGIS_ ...

  10. C# 进销存系统开发框架

    C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 视频下载: 百度 ...