讨论/《链表》 - 两数相加/
《链表》 - 两数相加

Python3

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        divdata = 0
        l3 = n = ListNode(0)
        if not l1:
            return l2
        if not l2:
            return l1
        while l1 or l2 or divdata:
            v1 = v2 = 0
            if l1 is not None:
                v1 = l1.val
                l1 = l1.next
            if l2 is not None:
                v2 = l2.val
                l2 = l2.next
            divdata, valdata = divmod(v1 + v2 + divdata,10)
            n.next = ListNode(valdata)
            n = n.next
        return l3.next
展开全部 14 讨论