Ch. 18 · Data Structures & Algorithms

Linked List Reversal and Pointer Ownership

Linked List Reversal and Pointer Ownership. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

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 previous
Text

This 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.

More in Data Structures & Algorithms

read ✓Data Structures & Algorithms · mid

Linked List Cycles and Fast-Slow Pointers

Linked List Cycles and Fast-Slow Pointers. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
read ✓Data Structures & Algorithms · mid

Bit Manipulation Patterns for Interviews

Set, test and clear bits, exploit XOR properties, and count set bits with the Brian Kernighan trick.

~2 min readread →
read ✓Data Structures & Algorithms · hard

Backtracking and Safe Pruning Rules

Backtracking and Safe Pruning Rules. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
esc