Python Solution


  • 0
    Q
    class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        p0 = p = l1
        q = l2
        carry = 0
        while(p and q):
            p.val += q.val + carry
            carry = p.val / 10
            p.val = p.val % 10
            p0 = p
            p = p.next
            q = q.next
    
        if p is None:
            p0.next = q
            p = q
    
        #计算进位
        while p:
            p.val += carry
            carry = p.val / 10
            p.val = p.val % 10
            p0 = p
            p = p.next
    
        if carry > 0:
            p0.next = ListNode(carry)
    
        return l1

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.