Another Python solution using recursion


  • 0
    R
    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            if l1 and l2:
                l = ListNode(l1.val + l2.val)
                if l.val < 10:
                    l.next = self.addTwoNumbers(l1.next, l2.next)
                    return l
                else:
                    l.val -= 10
                    l.next = self.addTwoNumbers(self.addTwoNumbers(l1.next, ListNode(1)), l2.next)
                    return l
            else:
                return l1 or l2
    

Log in to reply
 

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