educative.io

Question about this line

On line 31 , we code this swap using the Python shorthand:
curr_1.next, curr_2.next = curr_2.next, curr_1.next

If I were split the code into:
curr_1.next = curr_2.next
curr_2.next = curr_1.next
those don’t seem to work. I kinda understand this might create a circular reference, but how does the original line (curr_1.next, curr_2.next = curr_2.next, curr_1.next) work?

I think the right hand side, ‘curr_2.next, curr_1.next’, is a shorthand to create a tuple with two elements. The values are basically copies of curr_2.next, curr_1.next, respectively, not references.

The assignment unpacks the tuple and does multiple variable assignments in one shot.

I have the same problem.
If you try to do it without using the python shorthand for swapping variables it does not work.
That is because you need to store first the value in a variable before override it. Something like this:

    temp = current1.next
    current1.next = current2.next 
    current2.next = temp

Otherwise you enter in an infinite loop.