A short solution in Python


  • 0
    C
    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            ret = ListNode(0)
            v = 0
            r = ret
            while l1 or l2:
                v1 = l1.val if l1 else 0
                v2 = l2.val if l2 else 0
                v = v1 + v2 + r.val
                r.val = v % 10
                l1 = l1.next if l1 else l1
                l2 = l2.next if l2 else l2
                if l1 or l2 or v/10 > 0:
                    r.next = ListNode(v/10)
                    r = r.next
            return ret

Log in to reply
 

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