leetcode 148. Sort List ----- java
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的更多相关文章
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- Java for LeetCode 148 Sort List
Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...
- [LeetCode] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- 【Leetcode】Sort List JAVA实现
Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...
- [LeetCode] 148. Sort List 解题思路
Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...
- 148. Sort List (java 给单链表排序)
题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...
- LeetCode 148 Sort List 链表上的归并排序和快速排序
Sort a linked list in O(n log n) time using constant space complexity. 单链表排序----快排 & 归并排序 (1)归并排 ...
- Leetcode#148 Sort List
原题地址 链表归并排序 真是恶心的一道题啊,哇了好多次才过. 代码: void mergeList(ListNode *a, ListNode *b, ListNode *&h, ListNo ...
- [LeetCode]148. Sort List链表归并排序
要求时间复杂度O(nlogn),空间复杂度O(1),采用归并排序 传统的归并排序空间复杂度是O(n),原因是要用一个数组表示合并后的数组,但是这里用链表表示有序链表合并后的链表,由于链表空间复杂度是O ...
随机推荐
- java程序(一)----HashMap同时获取键值
快速会用: HashMap<Integer,String> maps=new HashMap<Integer,String>(); maps.put(1,"xiaom ...
- java枚举类
enum关键字用于定义枚举类,若枚举只有一个成员, 则可以作为一种单例模式的实现方式. 枚举类对象的属性不应允许被改动, 所以应该使用 private final 修饰. 枚举类的使用 priva ...
- 如何在redhat下安装办公软件(openoffice)
在redhat的client版本中自带有办公软件libreoffice,而在server版的redhat中却没有自带的办公软件,那么,如何在redhat的server版下安装办公软件呢? 方法一:配置 ...
- mysql 启动错误1026
进入“事件查看器”“应用程序”果然发现很多MySql的错误Default storage engine (InnoDB) is not available 于是进入MySql的安装目录找到my.ini ...
- [转][C/C++]函数名字修饰(Decorated Name)方式
1.C/C++函数修饰名: 对于我们的C/C++源程序而言,函数名只是函数的一小部分,函数还有调用方式(参数入栈方式).返回值类型.参数个数和各参数类型等信息,对于C++类成员函数,还有更多信息.这些 ...
- 显示pdf
document.all[document.all.PDFNotKnown ? "IfNoAcrobat" : "IfAcrobat"].style.displ ...
- 将salt取到的数据处理
#!/usr/bin/env python #coding:utf-8 import json with open('minfo') as f,open('minfoMiddle','w') as f ...
- java语法学习问题总结
No.1:EnumTest No.2:Addition 在此程序中,学习了将文本框调用出来,文本框输入的数据都是String类型,所以用于计算时需要先进行转型,然后计算. No.3:TestDoubl ...
- BZOJ 3999 旅游
.......好长啊. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
Android沉浸式(侵入式)标题栏(状态栏)Status(二) 附录1以xml写style实现了Android沉浸式(侵入式)状态栏(标题栏),同样以上层Java代码实现.在附录文章1的基础上 ...