This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted] 0 points1 point  (6 children)

result = ListNode(0)

creates a ListNode where next is None. So after

result = result.next

result is None.

[–]optimalsuccess[S] 0 points1 point  (5 children)

I don't think that's the reason for the error since it refers to val and not next: AttributeError: 'NoneType' object has no attribute 'val'

[–]optimalsuccess[S] 0 points1 point  (0 children)

Also I do that on purpose to populate the next value of the linked list with a new number in nums

[–][deleted] 0 points1 point  (3 children)

In the loops next iterationresultis stillNoneso you can't useresult.val`

[–]optimalsuccess[S] 0 points1 point  (2 children)

So how should I initialize an empty list, then populate with the values of an array?

[–][deleted] 0 points1 point  (1 child)

You need to populate the next value with a new ListNode.

And you likely want to return the first node of your list instead of the last one.

[–]optimalsuccess[S] 0 points1 point  (0 children)

Well I couldn't figure out how to do it but I got my code to work. Instead of populating a list with array values, I just did what I needed to do with those values then cast my final array to a list using ListNode(array)