python算法日记(链表系列)_leetcode 445. 两数相加 II

给定两个非空链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

进阶:

如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。

示例:

输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 8 -> 0 -> 7

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

 迭代,有时间精简下代码

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

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        if not l1 or not l2:
            return 
        c = a = b = []
        while(l1):
            a.append(l1.val)
            l1 = l1.next
        while(l2):
            b.append(l2.val)
            l2 = l2.next
        flag = 0
        while a and b:
            if flag==0:
                sums = a.pop()+b.pop()
            elif flag==1:
                sums = a.pop()+b.pop()+1
            if sums//10>=1:
                sums=sums%10
                flag = 1
            else:
                flag = 0
            c.append(sums)
        if flag==0:
            c+=a[::-1]
            c+=b[::-1]
            a = []
            b = []
        d = a or b
        while d:
            if flag==1:
                sums = 1+d.pop()
            elif flag==0:
                sums = d.pop()
            if sums//10>=1:
                sums=sums%10
                flag = 1
            else:
                flag = 0
            c.append(sums)
        if not d and flag==1:
            c+=[1]
        cur = res = ListNode(0)
        while c:
            val = c.pop()
            res.next = ListNode(val)
            res = res.next
        return cur.next

递归

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

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        cur = dummy = ListNode(0)
        cur1,cur2 = l1,l2
        while l1 and l2:
            l1 = l1.next
            l2 = l2.next
        if not l1 and not l2:
            ll1 = cur1
            ll2 = cur2
        elif l1:
            while l1:
                dummy.next=ListNode(0)
                dummy = dummy.next
                l1 = l1.next
            dummy.next=cur2
            ll2 = cur.next
            ll1 = cur1
        elif l2:
            while l2:
                dummy.next=ListNode(0)
                dummy = dummy.next
                l2 = l2.next
            dummy.next=cur1
            ll1 = cur.next
            ll2 = cur2

        def loop(l1,l2,carry):
            if not l1 and not l2:
                return 0
            carry = loop(l1.next,l2.next,carry)
            sums = l1.val+l2.val+carry
            carry = 1 if sums>9 else 0
            if sums//10==0:
                l1.val = sums
                carry = 0
            else:
                l1.val = sums%10
                carry = 1
            return carry
        ad = addhead = ListNode(1)
        if loop(ll1,ll2,0):
            addhead.next = ll1
            return ad
        else:
            return ll1

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值