I was just practicing some Linked List questions when I came across something I couldn't explain. So I was wondering if anyone could help me out.
If I have a Linked List with nodes implemented as follows
class Node:
def __init__(self, val, next):
self.val = val
self.next = next
I tried reversing the Linked List initially like so and it came up with an AttributeError: 'NoneType' object has no attribute 'next'
prev = None
curr = head
while curr:
prev, curr, curr.next = curr, curr.next, prev
And then I modified it to and it worked but I can't seem to wrap my head around it.
prev = None
curr = head
while curr:
curr.next, prev, curr = prev, curr, curr.next
I'm aware that Python evaluates the RHS as a tuple and then assigns the values on the LHS from left to right but I can't seem to understand why both code snippets wouldn't be the same if this is the case.