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

排序,要求是O(nlog(n))的时间复杂度和常数的空间复杂度,那么就使用归并就可以了。

/**
* 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 || head.next == null)
return head; int size = 1;
ListNode start = new ListNode(0);
start.next = head; while( true ){
ListNode node1 = start;
ListNode node2 = start.next;
for( int i = 0 ; i < size && node2!=null;i++){
node2 = node2.next;
} if( node2 == null )
break;
ListNode nnn = start.next; while( node2 != null ){
node1 = helper(node1,node2,size);
if( node1 == null )
break;
node2 = node1.next;
for( int i = 0 ; i< size && node2 != null;i++){
node2 = node2.next;
}
}
size*=2;
}
return start.next;
} public ListNode helper(ListNode node1,ListNode node2,int size){ int num1 = 0,num2 = 0; ListNode node = null; if( node1.next.val < node2.val ){
node = node1.next;
node1 = node1.next.next;
num1++;
}else{
ListNode nn = node1.next;
node1.next = node2;
node1 = nn;
node = node2;
node2 = node2.next;
num2++;
} while( num1 < size && num2 < size && node1 != null && node2 != null){ if( node1.val < node2.val ){
node.next = node1;
node = node1;
node1 = node1.next;
num1++;
}else{
node.next = node2;
node = node2;
node2 = node2.next;
num2++;
}
}
while( num1 < size && node1 != null){
node.next = node1;
node = node1;
node1 = node1.next;
num1++;
} while( num2 < size && node2 != null){
node.next = node2;
node = node2;
node2 = node2.next;
num2++;
}
node.next = node2;
return node; }
}

leetcode 148. Sort List ----- java的更多相关文章

  1. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

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

  2. Java for LeetCode 148 Sort List

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

  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. 【Leetcode】Sort List JAVA实现

    Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...

  5. [LeetCode] 148. Sort List 解题思路

    Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...

  6. 148. Sort List (java 给单链表排序)

    题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...

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

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

  8. Leetcode#148 Sort List

    原题地址 链表归并排序 真是恶心的一道题啊,哇了好多次才过. 代码: void mergeList(ListNode *a, ListNode *b, ListNode *&h, ListNo ...

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

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

随机推荐

  1. TOP 10开源的推荐系统简介

    最近这两年推荐系统特别火,本文搜集整理了一些比较好的开源推荐系统,即有轻量级的适用于做研究的SVDFeature.LibMF.LibFM等,也有重量级的适用于工业系统的 Mahout.Oryx.Eas ...

  2. oracle安装后登录

    运行 sqlplus回车 longon as sysdba回车 回车 这样就可以登录了. SQL>create user username identified by password; SQL ...

  3. NIO基础

    通道和缓冲区 概述 通道 和 缓冲区 是 NIO 中的核心对象,几乎在每一个 I/O 操作中都要使用它们. 通道是对原 I/O 包中的流的模拟.到任何目的地(或来自任何地方)的所有数据都必须通过一个 ...

  4. CentOS SSH配置

    默认CentOS已经安装了OpenSSH,即使你是最小化安装也是如此.所以这里就不介绍OpenSSH的安装了. SSH配置: 1.修改vi /etc/ssh/sshd_config,根据模板将要修改的 ...

  5. (转)深入浅出 iOS 之生命周期

    原文:http://www.cocoachina.com/applenews/devnews/2011/0817/3129.html 深入浅出 iOS 之生命周期 发布于:2011-08-17 10: ...

  6. hdu 2043

    Ps:自己写了以后又去看了下苏哥的....改进版的....学到东西,直接套用了. 代码: #include "stdio.h"#include "string.h&quo ...

  7. Java中的blank final

    Java allows the creation of blank finals, which are fields that are declared as final but are not gi ...

  8. java equals 和hashcode

    1  如果不知道怎么重载hashcode, eclipse自动代码生成工具会帮助你生成,大概的思路是设定一个int prim,  然后根据各个成员的值或者hashcode值进行某种运算即可,具体什么运 ...

  9. Reachability判断网络是否连接

    类似于一个网络状况的探针. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabili ...

  10. POJ 2385 DP

    题意:在苹果树下,初始在第一棵树下,告诉你在第几秒的时候,那棵树下会落下苹果,告诉最多能移动的次数,然后来回移动,求能得到的最大的苹果数目. 思路:三维DP,d[第i秒][已经移动j次][当前在(1, ...