Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Use a dummy node before the head. Walk through the list with a prev pointer. For each pair (first, second), rewire: prev.next = second, first.next = second.next, second.next = first. Advance prev to first (which is now the second node in the swapped pair).
A dummy node simplifies head swaps. Each pair swap only needs three pointer reassignments. The prev pointer ensures the preceding node always links to the new front of each swapped pair.
Updated Feb 2026