Sort List leetcode java
题目:
Sort a linked list in O(n log n) time using constant space complexity.
题解:
考虑到要求用O(nlogn)的时间复杂度和constant space complexity来sort list,自然而然想到了merge sort方法。同时我们还已经做过了merge k sorted list和merge 2 sorted list。这样这个问题就比较容易了。
不过这道题要找linkedlist中点,那当然就要用最经典的faster和slower方法,faster速度是slower的两倍,当faster到链尾时,slower就是中点,slower的next是下一半的开始点。
解决了这些问题,题目就很容易解出了。
代码如下:
1 public ListNode sortList(ListNode head) {
2 if(head == null|| head.next == null)
3 return head;
4 ListNode slow = head, fast = head, firsthalf = head;
5 while(fast.next!=null&&fast.next.next!=null){
6 slow = slow.next;
7 fast = fast.next.next;
8 }
9 ListNode secondhalf = slow.next;
slow.next = null;
ListNode leftlist = null, rightlist =null;
if(firsthalf!=secondhalf){
leftlist = sortList(firsthalf);
rightlist = sortList(secondhalf);
}
return mergeTwoLists(leftlist, rightlist);
}
public ListNode mergeTwoLists(ListNode leftlist, ListNode rightlist){
if(rightlist == null)
return leftlist;
if(leftlist == null)
return rightlist;
ListNode fakehead = new ListNode(-1);
ListNode ptr = fakehead;
while(rightlist!=null&&leftlist!=null){
if(rightlist.val<leftlist.val){
ptr.next = rightlist;
ptr = ptr.next;
rightlist = rightlist.next;
}else{
ptr.next = leftlist;
ptr = ptr.next;
leftlist = leftlist.next;
}
}
if(rightlist!=null)
ptr.next = rightlist;
if(leftlist!=null)
ptr.next = leftlist;
return fakehead.next;
}
Sort List leetcode java的更多相关文章
- Sort Colors leetcode java
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- Insertion Sort List Leetcode java
题目: Sort a linked list using insertion sort. 题解: Insertion Sort就是把一个一个元素往已排好序的list中插入的过程. 初始时,sorted ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- How to sort HashSet in Java
How to sort HashSet in Java 方法一:By Converting HashSet to List 方法二:By Converting HashSet to TreeSet i ...
- Sort List leetcode
这个题一开始本想用快速排序的,但是想了20分钟都没有头绪,难点在于快速排序的随机访问无法用链表实现,不过如果可以实现快速排序partition函数就可以了,但是这可能比较复杂,于是改用其他排序方法,上 ...
- sort学习 - LeetCode #406 Queue Reconstruction by Height
用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...
- Scramble String leetcode java
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
随机推荐
- 最详细的vue-cli安装教程 &^没有之一 ^& 大神亲测。。╮( ̄▽  ̄)╭
这里介绍使用git安装,电脑自带命令行依然可以使用进行安装 第一步 node环境安装 1.1 如果本机没有安装node运行环境,请下载node 安装包进行安装 1.2 如果本机已经安装node的运行换 ...
- 跟厂长学PHP7内核(二):源码分析的环境与工具
本文主要介绍分析源码的方式,其中包含环境的搭建.分析工具的安装以及源码调试的基本操作. 一.工具清单 PHP7.0.12 GDB CLion 二.源码下载及安装 $ wget http://php.n ...
- Xamarin 2017.10.9更新
Xamarin 2017.10.9更新 本次更新主要解决了一些bug.Visual Studio 2017升级到15.4获得新功能.Visual Studio 2015需要工具-选项-Xamarin ...
- kube-ui安装
kube-ui是k8s提供的web管理界面,可以展示节点的内存.CPU.磁盘.Pod.RC.SVC等信息. 1.编辑kube-dashboard-rc.yml定义文件[root@kubernetes- ...
- 深入剖析cpp对象模型
C++对象模型可以概括为以下2部分: 1. 语言中直接支持面向对象程序设计的部分,主要涉及如构造函数.析构函数.虚函数.继承(单继承.多继承.虚继承).多态等等. 2. 对于各种支持的底层实现机制.在 ...
- 【BZOJ 2318】 2318: Spoj4060 game with probability Problem(概率DP)
2318: Spoj4060 game with probability Problem Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 371 Sol ...
- [CEOI2008]order --- 最小割
[CEOI2008]order 题目描述: 有N个任务,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成,你可以通过购买或租用机器来完成. 现在给出这些参数, ...
- 【洛谷】1477:[NOI2008]假面舞会【图论】
P1477 [NOI2008]假面舞会 题目描述 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会. 今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具 ...
- 河南省队选拔 HAOI2015 解题报告
其实省选在四天前就已经结束了,但由于题目难度略大我到今天上午才补完所有题目……(捂脸逃)考场上很幸运,打完了所有我会写的部分分,最后Round1的110分 + Round2的70分,勉强算是没有被 ...
- Codeforces Round #283 (Div. 2) B. Secret Combination 暴力水题
B. Secret Combination time limit per test 2 seconds memory limit per test 256 megabytes input standa ...