leedcode 排序链表

本文介绍了一种在链表数据结构上实现的归并排序算法,通过递归方式将链表分为两半,然后对每半进行排序,最后合并两个已排序的链表。同时,还提供了一种快速排序的实现,尽管在某些情况下可能会导致超时。这两种排序算法都是在链表上操作,避免了传统数组排序中可能遇到的空间问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.归并排序

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def sortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        return self.mergeSort(head)
    def mergeSort(self, head):
        #处理特殊情况
        if head is None:
            return head
        if head.next is None:
            return head
        pNode = head.next
        if pNode.next is None:
            if head.val > pNode.val:
                temp = pNode.val
                pNode.val = head.val
                head.val = temp
            return head
        #寻找链表的中间节点
        pSlow = head
        pFast = head.next
        while pSlow and pFast:
            pSlow = pSlow.next
            pFast = pFast.next
            if pFast and pFast.next:
                pFast = pFast.next
            else:
                break
        #归并排序
        rightList = self.mergeSort(pSlow.next)
        pSlow.next = None
        leftList = self.mergeSort(head)

        # merge
        pHead = None
        pEnd = None
        if leftList.val <= rightList.val:
            pHead = leftList
            pEnd = pHead
            leftList = leftList.next
        else:
            pHead = rightList
            pEnd = pHead
            rightList = rightList.next
        while leftList and rightList:
            if leftList.val <= rightList.val:
                pEnd.next = leftList
                pEnd = pEnd.next
                leftList = leftList.next
            else:
                pEnd.next = rightList
                pEnd = pEnd.next
                rightList = rightList.next
        while leftList:
            pEnd.next = leftList
            pEnd = pEnd.next
            leftList = leftList.next
        while rightList:
            pEnd.next = rightList
            pEnd = pEnd.next
            rightList = rightList.next
        pEnd.next = None
        return pHead

2.快速排序(超时)

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def sortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        tail = head
        if head is None:
            return head
        while tail.next:
            tail = tail.next
        return self.QuickSort(head, tail)[0]
    def QuickSort(self, head, tail):
        # 处理特殊情况
        if head is None:
            return head, head
        if head.next is None:
            return head, head
        pNode = head.next
        if pNode.next is None:
            if head.val > pNode.val:
                temp = pNode.val
                pNode.val = head.val
                head.val = temp
            return head, head.next
        key = head.val
        p = head.next
        leftHead = None
        leftFront = None
        leftTail = None
        rightHead = None
        rightFront = None
        rightTail = None
        while p:
            if p.val < key:
                if leftHead is None:
                    leftHead = p
                    leftFront = leftHead
                else:
                    leftFront.next = p
                    leftFront = leftFront.next
            else:
                if rightHead is None:
                    rightHead = p
                    rightFront = rightHead
                else:
                    rightFront.next = p
                    rightFront = rightFront.next
            p = p.next
        if leftFront:
            leftFront.next = None
        if rightFront:
            rightFront.next = None
        leftTail = leftFront
        rightTail = rightFront
        if leftHead:
            leftHead, leftTail = self.QuickSort(leftHead, leftTail)
        if rightHead:
            rightHead, rightTail = self.QuickSort(rightHead, rightTail)
        subHead = None
        subEnd = None
        if leftHead and rightHead:
            leftTail.next = head
            head.next = rightHead
            head = leftHead
            subHead = head
            subEnd = rightTail
        elif leftHead:
            leftTail.next = head
            head.next = None
            subEnd = head
            head = leftHead
            subHead = head
        elif rightHead:
            head.next = rightHead
            subHead = head
            subEnd = rightTail
        return subHead, subEnd

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值