148. Sort List
题目:
Sort a linked list in O(n log n) time using constant space complexity.
链接: http://leetcode.com/problems/sort-list/
题解:
Sort List, 链表排序,一看到O(nlogn)就想到使用merge sort。 对于merge sort,还要研究一下bottom-up和top-down的区别,优劣点,如何继续优化等等,比如n较小时使用insertion sort, 了解Python的Tim-sort之类。也要研究一下链表的Quicksort,以及3-way Quicksort。 当时电面Amazon的时候写排序作死选择Quicksort,还尝试写了一个3-way Quicksort,由于理解不深同时当天也头昏脑胀没解释清楚,被面试官白大姐秒拒,如此浪费一年机会,甚是可惜。对于各种排序的stalability也要好好复习。熟能生巧,多练吧。另外,external sorting,B tree,A* tree,indexing,paging之类的也要复习。聊远了...
这题使用Merge Sort的话,还是按照Divide-and-Conquer分治。 两个辅助方法是findMid找中点,以及merge合并。 merge的话完全可以使用"Merge Sorted List"的代码,找中点我们也使用过,所以可以直接写。
Time Complexity - O(nlogn), Space Complexity - O(logn)。
/*** 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;ListNode mid = findMid(head);ListNode right = sortList(mid.next);mid.next = null;ListNode left = sortList(head);return merge(left, right);}private ListNode findMid(ListNode head) { if(head == null || head.next == null)return head;ListNode fast = head, slow = head;while(fast.next != null && fast.next.next != null) {fast = fast.next.next;slow = slow.next;}return slow;}private ListNode merge(ListNode left, ListNode right) {ListNode dummy = new ListNode(-1);ListNode node = dummy;while(left != null && right != null) {if(left.val < right.val) {node.next = left;left = left.next;} else {node.next = right;right = right.next;}node = node.next;}if(left != null)node.next = left;elsenode.next = right;return dummy.next;} }
到这里其实还没结束。仔细看题目要求constant space complexity。这样的话我们就不能使用递归了。
二刷:
O(1) space并不好写,此刷没有完成,也并没有尝试quicksort,很忧伤... 最后还是使用的merge sort recrusive的方法。 代码大都参考了Discuss区里zwangbo同学的。
Java:
Time Complexity - O(nlogn), Space Complexity - O(logn)。
/*** 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; ListNode fast = head, slow = head, preMid = head;while (fast != null && fast.next != null) {preMid = slow;fast = fast.next.next;slow = slow.next;}preMid.next = null;return merge(sortList(head), sortList(slow));}private ListNode merge(ListNode l1, ListNode l2) {if (l1 == null) return l2;if (l2 == null) return l1;if (l1.val < l2.val) {l1.next = merge(l1.next, l2);return l1;} else {l2.next = merge(l1, l2.next);return l2;}} }
三刷:
依然是使用之前的方法,找中点,再递归进行mergesort。
Java:
Time Complexity - O(nlogn), Space Complexity - O(logn)。
/*** 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;ListNode fast = head, slow = head, prev = head;while (fast != null && fast.next != null) {prev = slow;fast = fast.next.next;slow = slow.next;}prev.next = null;return mergeTwoSortedList(sortList(head), sortList(slow));}private ListNode mergeTwoSortedList(ListNode l1, ListNode l2) {if (l1 == null) return l2;if (l2 == null) return l1;ListNode dummy = new ListNode(-1);ListNode curNode = dummy, p1 = l1, p2 = l2;while (p1 != null && p2 != null) {if (p1.val < p2.val) {curNode.next = p1;p1 = p1.next;} else {curNode.next = p2;p2 = p2.next;}curNode = curNode.next;}curNode.next = (p1 != null) ? p1 : p2;return dummy.next;} }
Reference:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/7-Sort/merge-sort5.html
https://en.wikipedia.org/wiki/Merge_sort#Bottom-up_implementation
https://leetcode.com/discuss/10264/my-o-n-log-n-time-o-1-space-solution
https://leetcode.com/discuss/12014/my-iterative-merge-sort
https://leetcode.com/discuss/15420/java-solution-with-strict-o-1-auxiliary-space-complexity
https://leetcode.com/discuss/18210/i-think-my-java-solution-is-o-1-space
https://leetcode.com/discuss/19119/java-merge-sort-solution-with-o-1-memory
https://leetcode.com/discuss/24917/my-java-code-with-constant-space
https://leetcode.com/discuss/28594/bottom-recurring-with-space-complextity-time-complextity
https://leetcode.com/discuss/39867/java-solution-bottom-iterative-merge-with-detailed-comments
https://leetcode.com/discuss/1709/have-pretty-mergesort-method-anyone-speed-reduce-memory-usage
https://leetcode.com/discuss/92701/java-solution-using-merge-sort
https://leetcode.com/discuss/44369/java-merge-sort-solution
https://leetcode.com/discuss/29921/basically-seems-merge-sort-problem-really-easy-understand
https://leetcode.com/discuss/81455/java-quick-sort-fast-beats-98%25-also-includes-merge-sort-code
https://leetcode.com/discuss/81263/super-easy-understand-java-iterative-merge-sort-using-space
148. Sort List相关推荐
- LeetCode 148. Sort List--面试算法题--C++,Python解法
LeetCode 148. Sort List–面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大 ...
- 【归并排序+递归】LeetCode 148. Sort List
LeetCode 148. Sort List Solution1:我的答案 很多递归 /*** Definition for singly-linked list.* struct ListNode ...
- 【leetcode】148. Sort List
Sort a linked list in O(n log n) time using constant space complexity. 链表排序可以用很多方法,插入,冒泡,选择都可以,也容易实现 ...
- sort list java leetcode_[LeetCode] 148. Sort List Java
题目:Sort a linked list inO(n log n) time using constant space complexity. 题意及分析:要求使用o(nlogn)的时间复杂度和o( ...
- 148. Sort List 1
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- leetcode 148. Sort List | 148. 排序链表(最优解归并排序,O(1)空间)
题目 https://leetcode.com/problems/sort-list/ 题解 分析 就链表排序问题来说,它的解法可以有好多种.但它的最优解应该只有一种思路,即从下至上的归并,时间复杂度 ...
- leetcode 148. Sort List
https://leetcode.com/problems/sort-list/discuss/46714/Java-merge-sort-solution 链表初始化代码:ListNode* ori ...
- 继续过中等难度.0309
. 8 String to Integer (atoi) 13.9% Medium . 151 Reverse Words in a String 15.7% Mediu ...
- LeetCode 所有题目总结
文章目录 做题注意事项 题目分类 1.位运算 2.字符串题型 3.TopK 问题--最大堆/最小堆 4.链表 5.动态规划 easy Medium hard 6.贪心 7.树 8.图 9.数学题 10 ...
最新文章
- Mysql技术内幕——InnoDB存储引擎
- 人脸识别损失函数笔记
- SliverLight Web part
- WebSocket 详解
- VC创建DLL动态链接库及其调用
- windows系统nexus3安装和配置
- linux查看队列 msg,linux第10天 msg消息队列
- MATLAB绘图辅助操作
- 如何使用Orchard搭建敏捷个人的网站(1)
- mysql变红_数据库变成红色紧急
- IIS配置启动.net项目时报错:“/”应用程序中的服务器错误。
- hdoj3709(数位dp)
- LTE附着流程详解-UECapabilityInformation
- 高精度线性恒压源设计及电路分析
- c语言 北京时间转换utc时间_UTC时间转换成北京时间C语言函数代码
- Resetting a lost Admin password
- antd select 等组件可搜索问题
- 微信小程序:认证注册微信小程序之后,正确登录微信小程序后台管理
- C++ 控制台 升级打怪小游戏
- 邮政社招笔试题库_中国邮政招聘考试试题及答案--总括版