I am writing simple code to reverse a linked list and realised the assignments can be done on one line, which I found pretty cool:
def Reverse(head):
prev_node = None
curr_node = head
while curr_node:
prev_node, curr_node.next, curr_node = curr_node, prev_node, curr_node.next
return prev_node
But I noticed that the code fails if I reverse the order of assignments between curr_node.next on the LHS (corresponding with prev_node on the RHS) and curr_node on the LHS (...curr_node.next on the RHS)
def Reverse(head):
prev_node = None
curr_node = head
print(curr_node.data)
print(curr_node.next.data)
print(prev_node)
while curr_node:
prev_node, curr_node, curr_node.next = curr_node, curr_node.next, prev_node
return prev_node
The input is
1 2 3 4
The output is
1
2
None
But the following error is produced from the while loop (only on the second block of code; the first one runs fine)
prev_node, curr_node, curr_node.next = curr_node, curr_node.next, prev_node
AttributeError: 'NoneType' object has no attribute 'next'
The closest discussion I could find on the subject was here. Which says that the RHS is evaluated first, from left to right. Which I think means that curr_node is stored, then prev_node, then curr_node.next. Then they are assigned to prev_node, curr_node.next and curr_node, respectively. I don't see the difference between the first and second example. Am I missing something simple?
Does anyone know why the first example runs while the second one produces an error?