Python error "invalid literal for int() with base 10" with input [1],[1]


  • 0
    Z

    Hi all,

    I have this error for this problem, here is my code:

    class Solution:
        # @param {ListNode} l1
        # @param {ListNode} l2
        # @return {ListNode}
        def addTwoNumbers(self, l1, l2):
            a = str(l1).strip('[]').replace(',','').replace(' ','')
            b = str(l2).strip('[]').replace(',','').replace(' ','')
            c = str(int(a)+int(b))
            c = c[::-1]
            return map(int,c)
    

    I really don't understand, can anyone help me out?

    Thanks!


  • 0
    A

    the input l1 and l2 here are not strings. They are instance of class ListNode which was defined by the code at the begining.

    # Definition for singly-linked list.
    class ListNode:
        def __init__(self, x):
            self.val = x
            self.next = None
    

    Thus you can not use methods of string such as "strip" or "replace".


Log in to reply
 

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