Aptitude | GATE CS 1998 | Question 75
Let p be a pointer as shown in the figure in a single linked list.
What do the following assignment statements achieve ?
q: = p → next p → next:= q → next q → next:=(q → next) → next (p → next) → next:= q
Answer:
Explanation: Initially p points to i and q points to i+1.
- p ->next:= q ->next : i next points to i+2
- q ->next:=(q ->next) ? next : i+1 next points to i+3
- (p ->next) ->next:= q : i+2 next points to i+1
Deducing finally i->i+2->i+1->i+3.
So, it swaps the two nodes next to p in the linked list.



