Python solution,using str and list,187ms

  • 1
    class Solution:
    # @return a ListNode
    def addTwoNumbers(self, l1, l2):
        num1 = str(l1.val)
        num2 = str(l2.val)
            l1 =
            num1 = str(l1.val) + num1
            l2 =
            num2 = str(l2.val) + num2
        num3 = list(str(int(num1) + int(num2)))
        n1 = ListNode(int(num3.pop(0)))
        n = n1
        while num3:
            n = ListNode(int(num3.pop(0)))
   = n1
            n1 = n
        return n

  • 0

    I think it is OK to use the int() and str() to do the conversion, but it is better to do it bit by bit from lower order to higher order

Log in to reply

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