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. CodeForces 534D Program B

    Description On February, 30th n students came in the Center for Training Olympiad Programmers (CTOP) ...

  2. DotNetBar v12.4.0.2 Fully Cracked

    更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.4.0.2 如果遇到破解问题可以与我 ...

  3. SharePoint 2013 开发——获取用户配置文件属性内容(User Profile)

    博客地址:http://blog.csdn.net/FoxDave 本篇我们应用SharePoint CSOM(.NET)来读取用户配置文件的信息,个人开始逐渐倾向于客户端模型,因为不用远程登录到 ...

  4. Oracle Enterprise Metadata Management (简称OEMM,Oracle元数据管理)12.1.3.0.1已经发布

    在数据处理及数据仓库建设中,元数据管理是必不可少的,OEMM可以解决元数据管理过程中各种关键业务问题和技术挑战,其中包括如何元数据的统计信息,了解变更数据之后对下游的影响范围,而且OEMM站在业务的角 ...

  5. SimpleDateFormat()简单了解

    比如:SimpleDateFormat sdf1 = new  SimpleDateFormat("yyyy年MM天dd日 HH时mm分ss秒 一年中的第 D 天 一年中第w个星期 一月中第 ...

  6. 2013年7月份第4周51Aspx源码发布详情

    大型企业通用管理ERP源码  2013-7-26 [VS2010]2013.7.4更新内容:1.修复决策模式-客户等级不能保存问题.2.修复企业知识库有报错问题.3.修复运营模式-人力资源分析模块-在 ...

  7. 从对SAE的一次授权安全评估浅谈云安全

      EMail: jianxin#80sec.comSite: http://www.80sec.comDate: 2011-12-20From: http://www.80sec.com/ [ 目录 ...

  8. <button>使用注意问题

    最近在项目的上传功能下(IE8)发现了如下的错误: 2015-08-13 09:14:03,396 WARN   [WARN] [http-8080-5] : Handler execution re ...

  9. struts2DMI(动态方法调用)

    DMI(Dynamic Method Invoke)即动态,是strus2的一个特性,我们知道,在最开始学习strus2时,往往一个action中只有一个excute方法,比如说add,delete, ...

  10. hdoj-2025

    #include "stdio.h"#include "string.h"void sort(char ch[],int count[],int n,int f ...