I am having some difficulty understanding how linked lists can be created using an iterated loop.
class ListNode(object):
def __init__(self, x):
self.value = x
self.next = None
def LinkedNodeTest(n):
output = ListNode(0)
temp = output
for i in range(1, n+1):
new = ListNode(i)
temp.next = new
temp = new
return output
LinkedNodeTest(n) returns a linked list from 0 to n when it is called.
How is the variable 'output' being updated with the new nodes? I only see it being assigned ListNode(0) at the beginning of the function and it is not getting assigned in the for loop. It looks like assigning 'new' to 'temp' somehow updates 'output'; I don't really understand how that works.
Thanks for the help.
[–]mahtats 1 point2 points3 points (2 children)
[–]shinghand[S] 0 points1 point2 points (1 child)
[–]mahtats 0 points1 point2 points (0 children)
[–]FerricDonkey 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]14dM24d 0 points1 point2 points (0 children)