美文网首页
148. 链表排序

148. 链表排序

作者: 葡萄肉多 | 来源:发表于2019-11-27 23:08 被阅读0次

Sort a linked list in O(n log n) time using constant space complexity.
Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

思路

  1. 归并排序
    def sortList(self, head: ListNode) -> ListNode:
        def mergeTwoList(L1, L2):
            if not L1:
                return L2
            if not L2:
                return L1
            newHead = ListNode(0)
            L3 = newHead
            while L1 and L2:
                if L1.val < L2.val:
                    L3.next = L1
                    L1 = L1.next
                else:
                    L3.next = L2
                    L2 = L2.next
                L3 = L3.next
            if L1:
                L3.next = L1
            else:
                L3.next = L2
            return newHead.next

        if not head or not head.next:
            return head
        slow = head
        quick = head
        while quick.next and quick.next.next:
            slow = slow.next
            quick = quick.next.next
        right = slow.next
        slow.next = None
        left = self.sortList(head)
        right = self.sortList(right)
        return mergeTwoList(left, right)
  1. 快排
    def sortList2(self, head: ListNode) -> ListNode:
        def partition(head, end):
            if not head or not end or head == end:
                return
            l1 = head
            l2 = head.next
            pivot = head.val
            while l2 != end.next and l2:
                if l2.val < pivot:
                    l1 = l1.next
                    if l1 != l2:
                        tmp = l1.val
                        l1.val = l2.val
                        l2.val = tmp
                l2 = l2.next
            if head != l1:
                tmp = l1.val
                l1.val = head.val
                head.val = tmp
            return l1
        def sortHelper(head, end):
            if not head or not end or head == end:
                return
            node = partition(head, end)
            sortHelper(head, node)
            sortHelper(node.next, end)
            return head

        if not head or not head.next:
            return head
        end = head
        while end.next:
            end = end.next

        new_head = sortHelper(head, end)
        return new_head

相关文章

  • 链表续

    148. 排序链表

  • 148. 排序链表

    148. 排序链表

  • LeetCode-148-排序链表

    LeetCode-148-排序链表 148. 排序链表[https://leetcode-cn.com/probl...

  • 148 排序链表

    148. 排序链表要求:时间复杂度 O(NlogN), 空间O(1)采用归并排序我们使用快慢指针 来定位单链表中间的位置

  • 链表排序问题

    148. Sort List(链表归并排序)Sort a linked list in O(n log n) ti...

  • [LeetCode] 148. 排序链表

    148. 排序链表在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。示例:输入: 4->2...

  • python实现leetcode之148. 排序链表

    解题思路 计算长度找到中点一分为二,分开排序然后归并 148. 排序链表[https://leetcode-cn....

  • 归并的思想解决链表排序、多个链表合并问题

    1. 23.合并多个有序链表 与归并排序思想一致,两两合并。 2. 148. 排序链表 思路:与归并思想一致 快慢...

  • 148. 排序链表

    在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。 示例 1: 输入: 4->2->1->...

  • 148. 排序链表

    先使用快慢指针将找到链表的中点,然后将链表切分成左右两部分,然后对左右指针递归进行排序,最后归并两个已经排序的链表...

网友评论

      本文标题:148. 链表排序

      本文链接:https://www.haomeiwen.com/subject/uhkuwctx.html