【打卡】牛客网:BM12 单链表的排序

题目要求空间复杂度 O(n),时间复杂度O(nlogn) 。

在网上搜了选用归并排序(自己只会写冒泡排序):分析排序算法的时间复杂度和空间复杂度_冒泡排序空间复杂度_编码小哥的博客-CSDN博客

速看了下原理,上课讲过:

归并排序(Merge Sort)图解,归并排序算法-学到牛牛 - 知乎 (zhihu.com)

发现需要用递归,不会写,又搜了一篇代码:

python 归并排序(详解)_归并排序python_一叶知秋的BLOG的博客-CSDN博客

自己写的:

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类 the head node
# @return ListNode类
#
class Solution:
    def sortInList(self, head: ListNode) -> ListNode:
        # write code here
        # 判断递归的终止条件
        if head.next is None:
            return head

        # 计算长度
        tmp = head
        cnt = 0
        while tmp is not None:
            cnt += 1
            tmp = tmp.next  # 经常忘记写.next

        # 划分链表,即找到中间结点
        cnt = cnt // 2
        midhead = head
        while cnt != 0:
            cnt -= 1
            if cnt == 0:
                pre = midhead
                midhead = midhead.next
                pre.next = None  # 让左边的链表末端指向None
            else:
                midhead = midhead.next

        # 用递归来排序
        lefthead = self.sortInList(head)
        righthead = self.sortInList(midhead)

        # 合并两个链表
        anshead = ListNode(0)
        ans = anshead

        while lefthead is not None and righthead is not None:
            if lefthead.val <= righthead.val:
                anshead.next = ListNode(lefthead.val)
                lefthead = lefthead.next
                anshead = anshead.next  # 查了好久才查出来这里没有加上
            else:

                anshead.next = ListNode(righthead.val)
                righthead = righthead.next
                anshead = anshead.next  # 查了好久才查出来这里没有加上

        #Plan 1
        if lefthead is not None:
            anshead.next=lefthead #可能不太行 #检测通过了,原因推测是:后一层递归对链表进行排序,不会影响以往的递归的结果。如果是全局的链表,需要考虑后一层递归是否影响以往递归的结果。例如,记忆化递归。

        if righthead is not None:
            anshead.next=righthead

        #Plan 2
        # while lefthead is not None:
        #     anshead.next = ListNode(lefthead.val)
        #     lefthead = lefthead.next
        #     anshead = anshead.next

        # while righthead is not None:
        #     anshead.next = ListNode(righthead.val)
        #     righthead = righthead.next
        #     anshead = anshead.next

        return ans.next

评论区基本用数组的方法,而python自带.sort(),所以没有什么排序算法出现:

# @param head ListNode类 the head node
# @return ListNode类
#
class Solution:
    def sortInList(self , head ):
        # write code here
        list1 = []
        while(head):
            list1.append(head.val)
            head = head.next
        list1.sort()
        temp = ListNode(0)
        pHead = temp
        for i in list1:
            pHead.next = ListNode(i)
            pHead = pHead.next
        return temp.next

有一个用了快排,但是没过:

class Solution:
    def sortInList(self , head: ListNode):
        # write code here
        def quickSort(head) -> ListNode:
            if head == None&nbs***bsp;head.next == None:
                return head
            index = head.val
            lNode = None
            rNode = None
            mNode = None
            p = head
            # 插在头节点处
            while p != None:
                temp = ListNode(p.val)
                if p.val == index:
                    temp.next = mNode
                    mNode = temp
                elif p.val > index:
                    temp.next = rNode
                    rNode = temp
                else:
                    temp.next = lNode
                    lNode = temp
                p = p.next

            # 返回了头节点,且有可能为None
            l1 = quickSort(lNode) 
            l2 = getTail(mNode)
            l2.next = quickSort(rNode)
            if l1 == None:
                return mNode
            else:
                l1Tail = getTail(l1)
                l1Tail.next = mNode
                return l1
                
        def getTail(head):
            if head == None:
                return None
            while head.next != None:
                head = head.next
            return head
        return quickSort(head)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值