-2

I'm trying to understand why I get different output when I run the code on leetcode and in VSC. I solved the problem "Add Two Numbers" in Python with the following code:

from types import NoneType


class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution(object):
    def addTwoNumbers(self, l1, l2):
        str_l1=""
        str_l2=""
        if type(l1.val) is int:
          my_l1=[l1.val]
        elif type(l1.val) is NoneType:
          my_l1=[0]
        else:
          my_l1=l1.val

        if type(l2.val) is int:
          my_l2=[l2.val]
        elif type(l2.val) is NoneType:
          my_l2=[0]
        else:
          my_l2=l2.val

        for i in range(len(my_l1)):
          str_l1+=str(my_l1[len(my_l1)-i-1])
        
        for i in range(len(my_l2)):
          str_l2+=str(my_l2[len(my_l2)-i-1])
        
        num_l1=int(str_l1)
        num_l2=int(str_l2)
        somma=num_l1+num_l2
        str_somma=str(somma)
        list_somma=[]
        for i in range(len(str_somma)):
          list_somma.append(int(str_somma[len(str_somma)-i-1]))
        sol=ListNode(list_somma)
        return sol

And this is the example:

my_solution=Solution()
x=my_solution.addTwoNumbers(ListNode([2,4,3]),ListNode([5,6,4]))
print(x.val)

When I run the code in VSC I get the correct output [7,0,8], while when I run it in leetcode with the same input I get the wrong output [[7]]. How is that possible?

6
  • Had to look up what leetcode is: I just tried the playground and copied your code. For Python and Python3 the ouput is [7, 0, 8] Commented Aug 6, 2022 at 9:05
  • You can find solutions here and here. Commented Aug 6, 2022 at 9:33
  • I can't reproduce your problem. Maybe you run different code in VSC. Commented Aug 6, 2022 at 10:19
  • Python has special function to check type - isinstance(l1.val, int) but for None I would use is - like elif l1.val is None: Commented Aug 6, 2022 at 10:19
  • Python allow for negatie index and you can use str_somma[-i-1] instead of str_somma[len(str_somma)-i-1]. But you could simply use reversed(my_l1) and run without range() and len() like for item in reversed(my_l1): str_l1 += str(item) Commented Aug 6, 2022 at 10:25

1 Answer 1

0
  1. The expected solution is a linked list, so a simple print(x.val) on one node doesn't display it very helpfully.

You can make your own helper function to print the linked list, something like:

def print_ll(head):
    res = []
    n = head
    while n:
        res.append(n.val)
        n = n.next
    print(res)

If you use your solution and then do print_ll(x), you get [[7, 0, 8]]. Note, it's not [7, 0, 8] which is what you would expect.

The reason is that the correct solution to the problem is a linked list with 3 nodes, each of which has an integer value. But what your code gives is a single node, whose value is itself a list.

  1. Why does LeetCode show a value ([[7]]) which is different again?

Look at the input you give your code when you run in VSCode:

x=my_solution.addTwoNumbers(ListNode([2,4,3]),ListNode([5,6,4]))

The inputs to the function there are not the same as it will get in LeetCode. You are giving inputs which are single nodes with .val equal to a list. But on LeetCode, the inputs are linked lists with multiple nodes. So the result of this is not an accurate reflection of what happens when you run on LeetCode.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.