Python freestyle


  • 0
    J
    def add_two_numbers(l1,l2):
        # reverse l1 and l2
        n1 = []
        while l1:
            n1.append(l1.val)
            l1 = l1.next
        
        n2 = []
        while l2:
            n2.append(l2.val)
            l2 = l2.next
        n1 = sum([n1[i]*10**i for i in xrange(len(n1))])
        n2 = sum([n2[i]*10**i for i in xrange(len(n2))])
        
        m = str(n1+n2)[::-1]
        m = [int(i) for i in m]
        
        dummy = cur = ListNode(0)
        for i in xrange(1,len(m)):
            cur.next = ListNode(m[i])
            cur = cur.next
        return dummy.next
    

Log in to reply
 

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