[LeetCode] 148. Sort List (Linked List) - Using Quick Sort(小改动)
来源:互联网 发布:阿里云ecs克隆 编辑:程序博客网 时间:2024/05/16 17:30
Quick Sort 的 原始 Psuedo Code.
原Quick Sort算法,如果遇到Linked list/Array中有非常多相同的元素时,性能会非常差。
(假设现在输入的是10000个node.val = 1的linked list. 那每次循环,都是只剔除掉一个元素,那要10000次quicksort递归... Orz.)
因此在每次Paritition过程中,将链表分成3段, Left, Reference, Right。(其中Left 与 Right 可能为空。)
与参照值reference值相同的,全部放入reference这条链表中, 而这条链表在后续过程中是不需要继续递归的。
在Quick Sort的递归过程中,分别讨论Left, Right为空时的情况,来将三条链表链接到一起。
class ListNode(object): def __init__(self, x): self.val = x self.next = Noneclass Solution(object): def sortList(self, head): """ sort list using quick sort :type head: ListNode :rtype: ListNode """ if head is None: return None tail = self.get_tail(head) head, tail = self.quick_sort(head, tail) tail.next = None return head def quick_sort(self, head, tail): """ Sort in place :param head: :param tail: :return: """ if head is not tail: head_left, tail_left, head_ref, tail_ref, head_right, tail_right = self.quicksort_partition(head, tail) if head_left is None: # if there is no node in left part after partition head = head_ref else: head_left, tail_left = self.quick_sort(head_left, tail_left) head = head_left tail_left.next = head_ref if head_right is None: # if there is no node in right part after partition tail = tail_ref else: head_right, tail_right = self.quick_sort(head_right, tail_right) tail_ref.next = head_right tail = tail_right return head, tail def quicksort_partition(self, head, tail): reference = tail head_ref, tail_ref = reference, reference head_left, tail_left, head_right, tail_right = None, None, None, None sentinel = ListNode(None) # use sentinel to simplify the code sentinel.next = head node = sentinel while node.next is not tail: node = node.next if node.val > reference.val: # put node into right part if head_right is not None: tail_right.next = node tail_right = node else: # right part is empty head_right = node tail_right = node elif node.val < reference.val: # put node into left part if head_left is not None: tail_left.next = node tail_left= node else: # left part is empty head_left = node tail_left = node else: # put node into reference part tail_ref.next = node tail_ref = node return head_left, tail_left, head_ref, tail_ref, head_right, tail_right def get_tail(self, node): while node.next: node = node.next return node
0 0
- [LeetCode] 148. Sort List (Linked List) - Using Quick Sort(小改动)
- leetcode上的Sort a linked list using insertion sort
- Sort a linked list using insertion sort
- Sort a linked list using insertion sort
- leetcode_Sort a linked list using insertion sort.
- 148. Sort List leetcode list
- LeetCode 148. Sort List
- [LeetCode]148.Sort List
- [leetcode] 148.Sort List
- 148. Sort List LeetCode
- Leetcode 148. Sort List
- LeetCode *** 148. Sort List
- LeetCode 148. Sort List
- leetcode.148. Sort List
- LeetCode-148.Sort List
- [Leetcode] 148. Sort List
- 148. Sort List --- leetcode
- LeetCode - 148. Sort List
- java多线程的创建
- Binary String Matching
- session Storage、local Storage和IndexedDB的区别
- c#(winform)环境下使用动态链接库dll的详解
- 第10、11周项目1 简单派生类(3)
- [LeetCode] 148. Sort List (Linked List) - Using Quick Sort(小改动)
- JavaScript中的比较
- 程序员的自我修养 读书笔记02
- 产品需求文档到底该怎么写?
- python3爬虫得到的数据乱码并进行处理
- UVa - 11292 - The Dragon of Loowater ( 贪心 )
- Dylsim-V2X simulation 软件介绍
- 将大于整数m且紧靠m的k个素数存入数组xx传回(160514)
- C++——中的虚函数(Virtual function)