Python: Deconstruct to Lists then add


  • 0
    D

    This code took me a lot longer than it should of, but I finished it. Let me know what you think.

    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            list1 = []
            list2 = []
            while l1:
                list1.append(str(l1.val))
                l1 = l1.next
            while l2:
                list2.append(str(l2.val))
                l2 = l2.next
            n1,n2= "".join(list1), "".join(list2)
            n = int(n1)+int(n2)
            new_values = map(lambda x:int(x),str(n))
            l = ListNode(new_values[0])
            head = l                #keep track of the head and where we going
            for value in new_values[1:]:
                l.next = ListNode(None)
                l=l.next
                l.val=value
            return head
    

Log in to reply
 

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