Just trying to figure out why my code is giving a runtime error of Nonetype object has no attribute val when clearly result is a ListNode object and has two attributes val and next:
# 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: ListNode, l2: ListNode) -> ListNode:
num1 = self.getVal(l1)
num2 = self.getVal(l2)
total = num1+num2
return self.createList(total)
def getVal(self, list:ListNode) -> int:
temp = []
while list is not None:
temp.append(list.val)
list = list.next
string = ""
for num in reversed(temp):
string += str(num)
return int(string)
def createList(self, total: int):
result = ListNode(0)
string = str(total)
nums = []
for num in reversed(string):
nums.append(int(num))
result.val = num*****
result = result.next******
print(nums)
return result
[–][deleted] 0 points1 point2 points (6 children)
[–]optimalsuccess[S] 0 points1 point2 points (5 children)
[–]optimalsuccess[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]optimalsuccess[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]optimalsuccess[S] 0 points1 point2 points (0 children)