straightforward python solution


  • 0
    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            add = 0
            v = l1.val + l2.val
            if v > 9:
                v = v - 10
                add = 1
            head = ListNode(v)
            h = head
            l1 = l1.next
            l2 = l2.next
            while l1 or l2:
                v = add
                if l1: v+= l1.val
                if l2: v+= l2.val
                if v > 9:
                    v = v - 10
                    add = 1
                else:
                    add = 0
                tmp = ListNode(v)
                h.next= tmp
                h = tmp
                if l1: l1 = l1.next
                if l2: l2 = l2.next
            if add == 1:
                tmp = ListNode(1)
                h.next= tmp
            return head
    

Log in to reply
 

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