148. Sort List
来源:互联网 发布:js正则验证身份证号码 编辑:程序博客网 时间:2024/06/14 06:42
Sort a linked list in O(n log n) time using constant space complexity.
java
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode sortList(ListNode head) { if (head == null || head.next == null) { return head; } ListNode leftDummy = new ListNode(0); ListNode midDummy = new ListNode(0); ListNode rightDummy = new ListNode(0); ListNode left = leftDummy; ListNode mid = midDummy; ListNode right = rightDummy; ListNode node = getMid(head); while (head != null) { if (head.val < node.val) { left.next = head; left = left.next; } else if (head.val > node.val) { right.next = head; right = right.next; } else { mid.next = head; mid = mid.next; } head = head.next; } left.next = null; mid.next = null; right.next = null; ListNode start = sortList(leftDummy.next); ListNode end = sortList(rightDummy.next); ListNode result = combine(start, midDummy.next, end); return result; } private ListNode getMid(ListNode head) { if (head == null) { return head; } ListNode slow = head; ListNode fast = head.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } private ListNode combine(ListNode head, ListNode mid, ListNode end) { ListNode dummy = new ListNode(0); ListNode pre = dummy; if (head != null) { pre.next = head; } pre = getTail(pre); if (mid != null) { pre.next = mid; } pre = getTail(pre); if (end != null) { pre.next = end; } return dummy.next; } private ListNode getTail(ListNode head) { if (head == null) { return head; } while (head.next != null) { head = head.next; } return head; }}
python
"""Definition of ListNodeclass ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next"""class Solution: """ @param: head: The head of linked list. @return: You should return the head of the sorted linked list, using constant space complexity. """ def sortList(self, head): # write your code here if head == None or head.next == None: return head leftDummy, midDummy, rightDummy = ListNode(0), ListNode(0), ListNode(0) left, mid, right = leftDummy, midDummy, rightDummy node = self.getMid(head) while head is not None: if head.val < node.val: left.next = head left = left.next elif head.val > node.val: right.next = head right = right.next else: mid.next = head mid = mid.next head = head.next left.next, right.next, mid.next = None, None, None start = self.sortList(leftDummy.next) end = self.sortList(rightDummy.next) result = self.combine(start, midDummy.next, end) return result def getMid(self, head): if head == None or head.next == None: return head slow, fast = head, head.next while fast != None and fast.next != None: slow = slow.next fast = fast.next.next return slow def combine(self, head, mid, end): dummy = ListNode(0) pre = dummy if head is not None: pre.next = head pre = self.getTail(pre) if mid is not None: pre.next = mid pre = self.getTail(pre) if end is not None: pre.next = end return dummy.next def getTail(self, head): if head == None or head.next == None: return head while head.next != None: head = head.next return head
阅读全文
0 0
- Sort-----148. Sort List
- 148. Sort List leetcode list
- LeetCode 148. Sort List
- [LeetCode]148.Sort List
- 148. Sort List
- [leetcode] 148.Sort List
- 148. Sort List
- 148. Sort List LeetCode
- Leetcode 148. Sort List
- 148. Sort List
- 148. Sort List
- 148. Sort List
- 148. Sort List
- 148. Sort List
- 148. Sort List
- LeetCode *** 148. Sort List
- 148. Sort List
- LeetCode 148. Sort List
- LeetCode刷题(7)
- POJ
- 1701H2 王建瑜 171011 连续第一天总结
- c++ 建立链表并实现合并
- Oracle笔记九:表的创建及管理
- 148. Sort List
- Ubuntu下 chrome,Java 和tomcat 初次配置
- 基于动态规划的最长公共子序列实现(LCS)
- Android Launcher3分析——开篇
- bzoj 3675 [Apio2014]序列分割(斜率优化)
- The C Programming Language 练习题4-2
- 数据可视化神器Tableau学习小结2
- [解惑]JavaScript事件机制
- SQL SERVER 2014 安装总结