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. (转)VS2012网站发布详细步骤

    2.弹出网站发布设置面板,点击<新建..>,创建新的发布配置文件: 4. 在配置中,要选择“Release”——发布模式(Release   称为发布版本,它往往是进行了各种优化,使得程序 ...

  2. 浅谈Android系统的图标设计规范

    http://homepage.yesky.com/89/11620089.shtml 目前移动平台的竞争日益激烈,友好的用户界面可以帮助提高用户体验满意度,图标Icon是用户界面中一个重要的组成部分 ...

  3. Android开发需要注意的地方

          1.理解运用商场概略 开发者对商场状况的理解与APP的胜利紧密相连,往常,AppStore和GooglePlay能够说是挪动运用最为丰厚的运用生态,像苹果的下载计算表单会记载抢手运用的下载 ...

  4. LinkedIn第三方登录

    官方开发文档网址:https://developer.linkedin.com angularjs LinkedIn初始化 var apiKey='77n7z65hd7azmb';$(function ...

  5. window.showModalDialog()复制内容

    ShowModalDialog 打开的 页面上加入个 <span id="mySpan" name="mySpan" contentEditable=&q ...

  6. For Me ...

    一直有做博客的打算,但是这也看不上,那也看不上. 总想着做一个personal blog,界面,风格全由自己设计. 可时间上总是不充裕的. 可技术上总是欠火候的. 可内容上总是待斟酌的. 于是时间被我 ...

  7. 文字超出DIV的边框

    已经给div设置了高宽,但是文字还是会戳出div而不是换行 鼓捣了一下好像是因为这个原因 如果全是 aaaaaaaaaaaaaaaaaaaaa 这样的纯英文,那么测试的时候是不会换行的,因为浏览器认为 ...

  8. hadoop 分片与分块,map task和reduce task的理解

    分块:Block HDFS存储系统中,引入了文件系统的分块概念(block),块是存储的最小单位,HDFS定义其大小为64MB.与单磁盘文件系统相似,存储在 HDFS上的文件均存储为多个块,不同的是, ...

  9. log4j 1.2配置(转载)

    转载自:http://www.blogjava.net/kit-soft/archive/2009/08/28/292977.html 第一步:加入log4j-1.2.8.jar到lib下. 第二步: ...

  10. phpcms 如何获取文章

    请求地址http://127.0.0.1/phpcms/index.php?m=content&c=index&a=show&catid=6&id=8 先来判断地址对应 ...