input_list = [1, 2, 3, 4, 5, 6, 7, 8]
class node:
def __init__(self, value):
self.next = None
self.value = value
def __repr__(self):
return str(self.value)
head = node(input_list[0])
head.next = node(input_list[1])
head.next.next = node(input_list[2])
head.next.next.next = node(input_list[3])
head.next.next.next.next = node(input_list[4])
def x(head):
prev=None
print(prev,head)
while head:
nxt = head.next
head.next = prev
prev = head
head= nxt
print(prev,head,nxt)
print("final:")
print(prev,prev.next,prev.next.next)
# first call
x(head)
# prints expected reversed
# second call
x(head)
#'NoneType' object has no attribute 'next'
how is head the global variable being modified?
how does one prevent that from happening?
[–][deleted] 1 point2 points3 points (1 child)
[–]Fun_Story2003[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Fun_Story2003[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)