I'm trying to understand some code that reverses a linked list.
Here's how I'm constructing a linked list:
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def initialize():
start = Node(0)
prev = start
for i in range(1, 6):
cur = Node(i)
prev.next = cur
prev = cur
return start
Here's the correct code in the answer:
def reverse_list(head):
new_head = None
while head:
head.next, head, new_head = new_head, head.next, head
return new_head
And here's what I have:
def reverse_list(head):
new_head = None
while head:
new_head, head, head.next = head, head.next, new_head
return new_head
However, my code throws an AttributeError when reversing a linked list:
AttributeError: 'NoneType' object has no attribute 'next'
I'm using the exactly same replacements as the correct answer, only in a different order. Why does my code throw an error? Does order matter when it comes to one line variable reassignments?