LeetCode题目(Python实现):合并两个有序链表

题目

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例 :

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

想法一:新建一个链

算法实现

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        # 特判
        if l1 is None and l2 is None:
            return None
        elif l1 is None:
            return l2
        elif l2 is None:
            return l1
        # 标记头
        if l1.val <= l2.val:
            head = ListNode(l1.val)
            cur = head
            l1 = l1.next
        else:
            head = ListNode(l2.val)
            cur = head
            l2 = l2.next
            
        while l1 is not None and l2 is not None:
            if l1.val <= l2.val:
                cur.next = ListNode(l1.val)
                l1 = l1.next
            else:
                cur.next = ListNode(l2.val)
                l2 = l2.next
            cur = cur.next
        if l1 is None:
            cur.next = l2
        elif l2 is None:
            cur.next = l1
        return head

执行结果

执行结果 : 通过
执行用时 : 40 ms, 在所有 Python3 提交中击败了67.86%的用户
内存消耗 : 13.5 MB, 在所有 Python3 提交中击败了24.94%的用户
在这里插入图片描述

复杂度分析

  • 时间复杂度:O(n+m),最多需要遍历两个链表的所有元素。

  • 空间复杂度:O(1),
    所需的额外空间为常数级。

想法二:修改原链指向

算法实现

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        # 特判
        if l1 is None and l2 is None:
            return ListNode
        elif l1 is None:
            return l2
        elif l2 is None:
            return l1
        # 确定主链和副链
        if l1.val <= l2.val:
            head = l1
            mainl = l1
            oth = l2
        else:
            head = l2
            mainl = l2
            oth = l1

        while mainl.next is not None and oth is not None:
            if mainl.next.val >= oth.val:
                mid = mainl.next
                mainl.next = oth
                oth = mid
                mainl = mainl.next
            else:
                mainl = mainl.next
        if oth is not None:
            mainl.next = oth
        return head

执行结果

执行结果 : 通过
执行用时 : 32 ms, 在所有 Python3 提交中击败了95.80%的用户
内存消耗 : 13.2 MB, 在所有 Python3 提交中击败了25.14%的用户
在这里插入图片描述

复杂度分析

  • 时间复杂度:O(n+m)

  • 空间复杂度:O(1)

递归法

算法实现

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        if l1 is None:
            return l2
        elif l2 is None:
            return l1
        elif l1.val < l2.val:
            l1.next = self.mergeTwoLists(l1.next, l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1, l2.next)
            return l2

执行结果

在这里插入图片描述

复杂度分析

  • 时间复杂度:O(n+m),最多需要遍历两个链表的所有元素。

  • 空间复杂度:O(n+m)

迭代法

算法实现

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        prehead = ListNode(-1)

        prev = prehead
        while l1 and l2:
            if l1.val <= l2.val:
                prev.next = l1
                l1 = l1.next
            else:
                prev.next = l2
                l2 = l2.next            
            prev = prev.next

        prev.next = l1 if l1 is not None else l2
        return prehead.next

执行结果

在这里插入图片描述

复杂度分析

  • 时间复杂度:O(n+m),最多需要遍历两个链表的所有元素。

  • 空间复杂度:O(1)

小结

题目虽小,但是有很多需要注意的地方,刚开始按自己的想法设计的时候,发现有很多边界和特殊情况需要处理,所以写完后很繁琐。
看了官方的题解,就很精炼,看来对Python的语法还是不够熟悉,需要继续多加练习。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值