we have taken 3 pointers as
X, Y and Z.
i. X is pointing to Head
ii. Y pointing to the next node of X
initially Z is Null hence,
X->next = Z, will make head node last node
Z will point to the node pointed by X, and now X and Will point to the second node.
in next iteration
Y will point to 3rd node, 2nd node's pointer will point to the first node which was head earlier.
and so on..
PS: don't consider this part of code only, but consider the entire while loop