I’m attempting this leetcode question.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
num1 = []
n1 = l1
while n1.next:
num1.append(n1.val)
n1 = n1.next
n2 = l2
num2 = []
while n2.next:
num2.append(n2.val)
n2 = n2.next
num1 = ''.join([str(x) for x in num1][::-1])
num2 = ''.join([str(x) for x in num2][::-1])
num1 = int(num1)
num2 = int(num2)
When I run the code, I get this error
ValueError: invalid literal for int() with base 10: ''
^^^^^^^^^
num1 = int(num1)
Line 23 in addTwoNumbers (Solution.py)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ret = Solution().addTwoNumbers(param_1, param_2)
Line 50 in _driver (Solution.py)
_driver()
Line 61 in <module> (Solution.py)
As far as I can tell, this shouldn’t be happening, as these seem to be valid literals.
I’ve also tried converting to float first and then int, via
num1 = int(float(num1))
num2 = int(float(num2))
which gives this error
ValueError: could not convert string to float: ''
^^^^^^^^^^^
num1 = int(float(num1))
Line 23 in addTwoNumbers (Solution.py)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ret = Solution().addTwoNumbers(param_1, param_2)
Line 50 in _driver (Solution.py)
_driver()
Line 61 in <module> (Solution.py)
What am I missing?