Concise Python solution using built-in adding method and string


  • 0
    P
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        
        int1 = int2 = retint = 0
        while l1 is not None:
            int1 = int1 * 10 + l1.val
            l1 = l1.next
        while l2 is not None:
            int2 = int2 * 10 + l2.val
            l2 = l2.next
        retint = int1 + int2
        head = cur = ListNode(0)
        for c in str(retint):
            cur.next = cur = ListNode(int(c))
        return head.next
    

Log in to reply
 

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