[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