TypeError: 'ListNode' object is not iterable for index,i in enumerate(l1) in Line 5

Viewed 20

i was practicing coding on Leetcode & i stumbled into the problem named Add two numbers. it has two lists which resembles two integers that listed backwardly. you have to add those two numbers & return the list of those integers as list in backwards. for example:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

first i try my work on Jupitar notebook & then i submit on Leetcode. this solution of mine works perfectly on any notebook but it doesn't run on Leetcode & shows an Error:

TypeError: 'ListNode' object is not iterable
    for index,i in enumerate(l1):
Line 5 in addTwoNumbers (Solution.py)
    ret = Solution().addTwoNumbers(param_1, param_2)
Line 35 in _driver (Solution.py)
    _driver()
Line 45 in <module> (Solution.py)

here's my solution:

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        list1 = 0
        list2 = 0
        for index,i in enumerate(l1):
            list1+=i*(10**(index))
        for index,i in enumerate(l2):
            list2+=i*(10**(index))
        list3 = list1+list2
        return [int(i) for i in str(list3)][::-1]
        
0 Answers
Related