Skip to Content
Course content

251: Whiteboard Practice: Reversing a Linked List In Place

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Look, if you've spent any time prepping for technical interviews, you know that reversing a linked list is practically a rite of passage. It's a classic because it tests whether you can visualize pointers moving in your head without getting tangled up. If you try to do this by just "changing the next pointer," you'll realize very quickly that you've just accidentally deleted the rest of your list from memory. That's the trap.

The Human Chain Analogy

Imagine a line of people standing in a row. Each person has their right hand resting on the shoulder of the person in front of them. Person A is holding Person B, B is holding C, and so on. Now, I want you to reverse this chain so that Person C is holding Person B, and Person B is holding Person A.

Here is the problem: the moment Person B lets go of Person C's shoulder to reach back and grab Person A, Person C is now standing there alone. If no one else was holding onto Person C, they've effectively vanished from the chain. You've lost your reference to the rest of the line.

To fix this, you need a third person—a "spotter"—to keep a finger on Person C's shoulder while Person B is busy switching their grip. Once Person B is securely holding Person A, the spotter can move forward to Person D, and the process repeats.

Mapping the Spotter to Java

In code, those three roles—the person behind, the person moving, and the spotter—map directly to three pointers: prev, curr, and nextTemp.

public ListNode reverseList(ListNode head) {
    ListNode prev = null;     // The person behind (starts as null because the new tail points to nothing)
    ListNode curr = head;     // The person currently switching their grip

    while (curr != null) {
        ListNode nextTemp = curr.next; // The Spotter: Hold onto the rest of the list!
        curr.next = prev;              // The Flip: Let go of the front, grab the person behind
        prev = curr;                   // Move the 'behind' marker forward to the current person
        curr = nextTemp;               // Move the 'current' marker to the spotter's position
    }
    
    return prev; // By the end, prev is the new head of the list
}

Why this feels weird at first

The most confusing part is usually the line curr.next = prev;. It feels counterintuitive to point "backward" when you're used to traversing a list "forward." But that's exactly what a reverse is. You are effectively rewriting the history of the list as you move through it.

I always tell my juniors: don't try to memorize these four lines of code. Instead, draw three boxes on a whiteboard and physically move your finger from one to the other. If you can visualize the "spotter" saving the next node before you break the link, you'll never forget the logic, even if you're nervous in an interview.




📋 Practical Task

Exercise: Implementing a Reverse-Order Undo Stack

You are building a simple text editor. You have a singly linked list that stores "Action" objects (each action represents a change made to the document). Currently, the list stores actions in the order they were performed (Oldest → Newest).

To implement an "Undo" feature, you need to process these actions in reverse order (Newest → Oldest) to roll them back. Instead of using extra memory to create a new list or an array, write a method called prepareUndoChain() that reverses the ActionNode list in place.

Requirements:

  • Modify the existing ActionNode pointers so the list is reversed.
  • The method must return the new head of the reversed list.
  • Ensure the original head of the list now points to null to avoid creating a circular reference.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.