Advanced
Node Swapping
Swap by changing LINKS, not data values โ this is what exams test
How Node Swapping Works
To swap two nodes by changing links:
- Find
currXandprevX - Find
currYandprevY - If either not found โ return
- Link
prevX โ currY(or update head) - Link
prevY โ currX(or update head) - Swap their
nextpointers
Before Swap
10
โ20
โ30
โ40
โ50
20 and 40 highlighted for swap
After Swap
10
โ40
โ30
โ20
โ50
โ Nodes swapped by links
node-swapping