Share my concise python solution


  • 0
    Y
        def addTwoNumbers(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            head = None
            carry = 0
            s1, s2 = [], []
            while l1:
                s1.append(l1)
                l1 = l1.next
            while l2:
                s2.append(l2)
                l2 = l2.next
            while s1 or s2 or carry:
                if s1:
                    carry += s1.pop().val
                if s2:
                    carry += s2.pop().val
                carry, rem = divmod(carry, 10)
                node = ListNode(rem)
                node.next = head
                head = node
            return head
    

Log in to reply
 

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