class ListNode:
def __init__(self,val,next=None) -> None:
self.val=val
self.next=next
n3=ListNode(3,n4)
n2=ListNode(2,n3)
n1=ListNode(1,n2)
n2,n2.next,n1=n2.next,n1,n2
if it happen at once, n2.next should be n1, but result shows n2.next=n3. Does it mean n2.next=n1 is executed after n2=n2.next is applied? what did i get wrong?
In python assignment statements, the right side is always evaluated before the values are assigned to the Left side. The result of each variable on your Right hand side i.e. n2.next,n1,n2 is evaluated such that n2.next is 3, n1 is 1 and n2 is 2. The assignment then happens from left to right.
n2.next is first assigned n2, n1 is then assigned to n2.next and n2 is then assigned to n1. So you are right, it proceeds in an order and it is not simultaneous.