One easy to understand Python solution


  • 0
    K
    class Solution:
    # @param {ListNode} l1
    # @param {ListNode} l2
    # @return {ListNode}
    def addTwoNumbers(self, l1, l2):
        dummy = temp = ListNode(0)
        carry = 0
        while l1 or l2 or carry:
            res = 0
            if l1:
                res += l1.val 
                l1 = l1.next
            if l2: 
                res += l2.val
                l2 = l2.next
            res += carry
            temp.next = ListNode(res % 10)
            temp = temp.next
            carry = res // 10
        return dummy.next

Log in to reply
 

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