LeetCode: Sort List 解题报告
Sort List
Sort a linked list in O(n log n) time using constant space complexity.
使用Merge Sort, 空间复杂度是 O(logN) 因为使用了栈空间。

SOLUTION 1:
使用Merge Sort来解决问题。
为什么不用QuickSort?
因为随机访问对于链表而言太耗时,而heap sort不可行。
注意,Find Mid用了2种解法。或者是让Fast提前结束,或是让Fast先走一步,目的就是要取得中间节点的前一个。这样做的目的,主要
是为了解决:
1->2->null
这一种情况。如果不这么做,slow会返回2.这样我们没办法切割2个Node的这种情况。
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode sortList(ListNode head) {
// Nodes should be more than 2.
if (head == null || head.next == null) {
return head;
} // get the mid node.
ListNode midPre = getMidPre(head); // Cut the two list.
ListNode right = midPre.next;
midPre.next = null; // Sort the left side and the right side.
ListNode left = sortList(head);
right = sortList(right); // Merge the two sides together.
return merge(left, right);
} // get the pre node before mid.
public ListNode getMidPre1(ListNode head) {
ListNode slow = head;
ListNode fast = head; while (fast != null && fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
} return slow;
} // get the pre node before mid.
public ListNode getMidPre(ListNode head) {
ListNode slow = head; // fast提前一点儿。这样就可以得到前一个节点喽。
ListNode fast = head.next; while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
} return slow;
} public ListNode merge(ListNode head1, ListNode head2) {
ListNode dummy = new ListNode(0);
ListNode cur = dummy; while (head1 != null && head2 != null) {
if (head1.val < head2.val) {
cur.next = head1;
head1 = head1.next;
} else {
cur.next = head2;
head2 = head2.next;
} cur = cur.next;
} if (head1 != null) {
cur.next = head1;
} else {
cur.next = head2;
} return dummy.next;
}
}
SOLUTION 2:
使用快排也可以解决。但是注意,要加一个优化才可以过大数据,就是判断一下是不是整个链条是相同的节点,比如2 2 2 2 2 2 2 ,这样的就直接扫一次不用执行
快排,否则它会是N平方的复杂度。
参考资料:
https://oj.leetcode.com/discuss/3577/i-use-quick-sort-to-sort-the-list-but-why-i-get-time-limited
/*
The Solution 2:
Quick Sort.
*/
public ListNode sortList(ListNode head) {
if (head == null) {
return null;
} // Sort the list from 0 to len - 1
return quickSort(head);
} // The quick sort algorithm // All the elements are the same!
public boolean isDuplicate(ListNode head) {
while (head != null) {
if (head.next != null && head.next.val != head.val) {
return false;
} head = head.next;
} return true;
} public ListNode quickSort(ListNode head) {
if (head == null) {
return null;
} // 如果整个链是重复的,直接跳过。
if (isDuplicate(head)) {
return head;
} // Use the head node to be the pivot.
ListNode headNew = partition(head, head.val); // Find the pre position of the pivoit.
ListNode cur = headNew; ListNode dummy = new ListNode(0);
dummy.next = headNew; ListNode pre = dummy; // Find the pre node and the position of the piviot.
while (cur != null) {
if (cur.val == head.val) {
break;
} // move forward.
cur = cur.next;
pre = pre.next;
} // Cut the link to be three parts.
pre.next = null; // Get the left link;
ListNode left = dummy.next; // Get the right link.
ListNode right = cur.next;
cur.next = null; // Recurtion to call quick sort to sort left and right link.
left = quickSort(left);
right = quickSort(right); // Link the three part together. // Link the first part and the 2nd part.
if (left != null) {
dummy.next = left; // Find the tail of the left link.
while (left.next != null) {
left = left.next;
}
left.next = cur;
} else {
dummy.next = cur;
} cur.next = right; // The new head;
return dummy.next;
} // Return the new head;
public ListNode partition(ListNode head, int x) {
if (head == null) {
return null;
} ListNode dummy = new ListNode(0);
dummy.next = head; ListNode pre = dummy;
ListNode cur = head; // Record the big list.
ListNode bigDummy = new ListNode(0);
ListNode bigTail = bigDummy; while (cur != null) {
if (cur.val >= x) {
// Unlink the cur;
pre.next = cur.next; // Add the cur to the tail of the new link.
bigTail.next = cur;
cur.next = null; // Refresh the bigTail.
bigTail = cur; // 移除了一个元素的时候,pre不需要修改,因为cur已经移动到下一个位置了。
} else {
pre = pre.next;
} cur = pre.next;
} // Link the Big linklist to the smaller one.
pre.next = bigDummy.next; return dummy.next;
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/SortList.java
LeetCode: Sort List 解题报告的更多相关文章
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- 【LeetCode】75. Sort Colors 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...
- 【LeetCode】148. Sort List 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】791. Custom Sort String 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...
随机推荐
- 头文件dirent.h
<dirent.h>是POSIX.1标准定义的unix类目录操作的头文件,包含了许多UNIX系统服务的函数原型,例如opendir函数.readdir函数. opendir函数: DI ...
- Java多线程中run(), start(), join(), wait(), yield(), sleep()的使用
Run 每个Thread中需要实现的方法, 如果直接调用的话, 会是和单线程一样的效果, 要另起线程需要使用start(). start 新起线程调用run(). 主线程不等待直接往下执行 Yield ...
- TCP/IP协议栈--IP首部选项字段的分析
IP输入函数(ipintr)将在验证分组格式(检验和,长度等)之后.确定分组是否到达目的地之前,对选项进行处理. 这表明分组所 遇到的每一个路由器以及终于的目的主机都对要分组的选项进行处理. IP分组 ...
- JavaScript的技巧和最佳实践
JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发 (Node.js和Wakanda)等等.JavaScript还 ...
- iOS开发-使用宏自定义输出(NSLog)
前言: 1)输出日志是会大量损耗系统性能 2)输出的信息很容易会被截取到,导致信息不安全. 所以我们会在发行版(Release)取消所有的Log.如果一行一行地去注释掉Log,显然不是一个明确的选择. ...
- ASP.NET#在设计窗口上添加了一个SqlDataSource控件后,没有显示出来?
在设计窗口上添加了一个SqlDataSource控件后,没有显示出来,但后台代码是有的 处理的办法:菜单栏->视图->可视辅助->ASP.NET非可视控件 (我用的是VS2012)
- centos7安装MySQL5.7无法设置密码问题
前言 在使用centos7系统yum方式安装MySQL5.7后 不知道默认密码是多少 知道后没办法修改? 一.找到MySQL密码 service mysqld start vim /var/log/ ...
- 在sys用户下执行的sql脚本创建了摁多个表和序列, 怎么回退?
一个个删除, 暂时不会别的方法...
- HDUOJ----1301 Jungle Roads
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- [转]动态加载javascript
动态加载script到页面大约有俩方法 第一种就是利用ajax方式,把script文件代码从后台加载到前台,然后对加载到的内容通过eval()执行代码. 第二种是,动态创建一个script标签,设置其 ...