Python beat 100%


  • -1
    J
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        return self.intToList(self.listToInt(l1) + self.listToInt(l2))
    
    def listToInt(self, l):
        res = 0
        ten = 1
        tmp = l
        while(tmp != None):
            res = res + tmp.val*ten
            ten = ten * 10
            tmp = tmp.next
        return res
        
    def intToList(self, x):
        init = ListNode(x % 10)
        tmp = init
        x = x / 10
        while( x / 10 != 0):
            tmp.next = ListNode(x%10)
            tmp = tmp.next
            x = x / 10
        if(x != 0):
            tmp.next = ListNode(x)
        return init

Log in to reply
 

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