Python solution


  • 0
    D
    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            result = ListNode(0)
            root = result
            while(l1!=None or l2!=None):
                l1 = l1 if l1 is not None else ListNode(0)
                l2 = l2 if l2 is not None else ListNode(0)
                result.val += l1.val + l2.val
                if(result.val>=10):
                    result.val %= 10
                    result.next = ListNode(1)
                elif(l1.next!=None or l2.next!=None):
                    result.next = ListNode(0)
                
                result = result.next
                
                l1 = l1.next if l1 is not None else None
                l2 = l2.next if l2 is not None else None
            return root
    

Log in to reply
 

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