LeetCode第二题两数相加(Python)

LeetCode第二题两数相加(Python)

题目描述

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题方法和思路

这个题目的难点在于进位的边界条件的考虑是否充分。代码有点冗余,但是思路不难。
我自己的解题思路:

  1. 首先判断两个链表的长度,确定较长的那个链表用来存储结果(省去了构建结果链表的麻烦)。分成长度大于,小于,等于考虑。(大于和小于的代码其实一样)
  2. 公共部分相加,考虑好进位。
  3. 较长部分相加,考虑好进位。(特别要将较长部分是99999时,并且进位是1的情况考虑进去)
    还有一个比较麻烦的地方是要考虑好链表游标的问题,单向链表下一个原始容易获得,但是上一个元素的获得会比较麻烦。
    核心代码:
class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        def get_length(l1:ListNode) -> int:
            length = 0
            while l1 is not None:
                length += 1
                l1 = l1.next
            return length
        length_l1 = get_length(l1)
        length_l2 = get_length(l2)
        
        head_l1 = l1
        head_l2 = l2
        temp = 0
        if length_l1 > length_l2:
            # l1和l2公共部分
            while l2 is not None:
                temp_result = l1.val + l2.val + temp
                if temp_result >= 10:
                    l1.val = temp_result - 10
                    temp = 1
                else:
                    l1.val = temp_result
                    temp = 0
                l1 = l1.next
                l2 = l2.next
            # l1部分
            while l1 is not None:
                temp_result = l1.val + temp
                if temp_result >= 10:
                    l1.val = temp_result - 10
                    temp = 1
                else:
                    l1.val = temp_result
                    temp = 0
                if l1.next is None:
                    tail_l1 = l1
                l1 = l1.next
            if temp == 1:
                tail_l1.next = ListNode(1)
            return head_l1
          
            
        if length_l1 < length_l2:
            # l1和l2公共部分
            while l1 is not None:
                temp_result = l1.val + l2.val + temp
                if temp_result >= 10:
                    l2.val = temp_result - 10
                    temp = 1
                else:
                    l2.val = temp_result
                    temp = 0
                l1 = l1.next
                l2 = l2.next
            # l2部分
            while l2 is not None:
                temp_result = l2.val + temp
                if temp_result >= 10:
                    l2.val = temp_result - 10
                    temp = 1
                else:
                    l2.val = temp_result
                    temp = 0
                if l2.next is None:
                    tail_l2 = l2
                l2 = l2.next
            if temp == 1:
                tail_l2.next = ListNode(1)
            return head_l2
        if length_l1 == length_l2:
            while l1 is not None:
                temp_result = l1.val + l2.val + temp
                if temp_result >= 10:
                    l2.val = temp_result - 10
                    temp = 1
                else:
                    l2.val = temp_result
                    temp = 0
                if l2.next is None:
                    tail_l2 = l2
                l1 = l1.next
                l2 = l2.next
            if temp == 1:
                tail_l2.next = ListNode(1)
                return head_l2
            else:
                return head_l2

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值