Naive Python Solution


  • 3
    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            c1, c2 = '', ''
            while l1:
                c1 += str(l1.val)
                l1 = l1.next
            while l2:
                c2 += str(l2.val)
                l2 = l2.next
            num = str(int(c1) + int(c2))
            dummy = ListNode(0)
            c = dummy
            for i in range(len(num)):
                c.next = ListNode(num[i])
                c = c.next
            return dummy.next

  • 0
    L

    Maybe you shouldn't even consider this as a naive solution, because the spirit of this exercise is to show how large numbers are added when there is a bit limit (I think), therefore int(str(huge_number)) already fails the purpose


  • 0

    @lucidsushi How did you know the "sprit of this excise"?


  • -1
    L

    @lilixu Someone told me about it when I shared them the original question (this one is part2). But you can imagine if a language had a method called add_two_linkedlist. Is there really a point to show the solution is return add_two_linkedlist(l1, l2)? You can check out the editoral comments for what the spirit of the question is https://leetcode.com/articles/add-two-numbers/


  • -1
    A

    @lilixu I agree with lucidsushi's opinion. According to the level of difficulty of this question, a medium level, one of test points should be how to manipulate singly linked lists to achieve the goal of summation of two lists; the other one point to check is how to use dfs idea to design your algorithm for this question. Your algorithm design is pretty easy, because all built-in Python functions you use in your code hide the test points. If you are preparing for technical interview, you should try not to use built-in Python functions in your solution. For easy level questions, you could use them.


Log in to reply
 

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