Python recursion


  • 0
    A
    class Solution(object):
    
        def addTwoNumbers(self, l1, l2, carry=0):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            if l1 is None:
                l1 = ListNode(0)
            if l2 is None:
                l2 = ListNode(0)
            c = 1 if (l1.val + l2.val + carry) >= 10 else 0
            val = (l1.val + l2.val + carry) % 10
            if l1.next or l2.next or c > 0:
                node = ListNode(val)
                node.next = self.addTwoNumbers(l1.next, l2.next, c)
                return node
            else:
                return ListNode(val)
    

Log in to reply
 

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