-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Java
-
Section 4: Collections Framework
-
Section 5: Exception Handling
-
Section 6: Generics
-
Section 7: Functional Java
-
Section 8: Concurrency
-
Section 9: I/O and NIO
-
Section 10: JVM Internals
-
Section 11: Modern Java Features
-
Section 12: Build Tools and Project Structure
-
Section 13: Testing
-
Section 14: Databases and Persistence
-
Section 15: Networking
-
Section 16: Design and Best Practices
-
Section 17: Reflection and Annotations
-
Section 18: Logging and Diagnostics
-
Section 19: Date, Time, and Internationalization
-
Section 20: Java Platform Module System
-
Section 21: Security in Java
-
Section 22: Advanced Collections and Data Structures
-
Section 23: More Concurrency Patterns
-
Section 24: Compression, Files, and System Integration
-
Section 25: GUI Programming
-
Section 26: Practical Projects
-
Section 27: Data Structures and Algorithms
-
Section 28: Interview and Algorithm Practice
-
Section 29: JSON and Data Interchange
-
Section 30: More Concurrency Utilities
-
Section 31: More Collections and Streams Practice
-
Section 32: More File and System Programming
-
Section 33: Standard Library Deep Dive
-
Section 34: More Practice and Drills
-
Section 35: More Testing and Quality
-
Section 36: More Design Patterns and Architecture
-
Section 37: Career and Ecosystem
-
Section 38: More OOP and Architecture Practice
-
Section 39: More Enterprise Concepts
-
Section 40: Advanced JavaFX
-
Section 41: More Interview Practice
251: Whiteboard Practice: Reversing a Linked List In Place
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
ActionNodepointers 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
nullto avoid creating a circular reference.
There are no comments for now.