In-place reversal rewires next references while preserving access to the unprocessed suffix. Keep previous, current and next roles distinct.
Before you start
You should know arrays, loops and basic complexity notation. Start with a small input you can trace by hand. State what each variable means and which invariant is maintained, then use that invariant to explain correctness before discussing performance or writing a more compact solution.
The practical goal is to reason through this situation: Save current.next before pointing current.next to previous. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Name pointer roles
Previous heads the reversed prefix; current heads the untouched suffix.
Step 2: Save continuation
Read current.next before changing that edge.
Step 3: Advance ownership
Reverse the edge, then move previous and current forward.
Worked scenario
Save current.next before pointing current.next to previous.
previous = null
current = head
while current is not null:
next = current.next
current.next = previous
previous = current
current = next
return previousThis is pseudocode. Saving next preserves access to the remainder; after the loop, previous is the new head.
Common mistake
Rewriting next before saving it loses the remaining list.
Verify the behavior
Trace three nodes edge by edge, then test empty and single-node lists.
Interview exercise
Explain the invariant.
Answer and reasoning
Previous heads the reversed prefix, current starts the unprocessed suffix and the saved next preserves continuation.
Continue learning
Compare the scenario with the Data Structures and Algorithms interview questions and test your understanding with the Data Structures and Algorithms MCQs. For terminology and implementation details, consult the reference material.