A Linked List is like a chain of elements where each item (called a Node) has a value and a pointer pointing to the next node. Reversing this list is one of the most classic coding interview questions designed to test how well you manipulate memory references.
Let's understand how pointer shifting works using a simple playground game analogy.
Imagine a line of five children playing a game. Each child places their hands on the shoulders of the child **in front of them** (pointing to the next node):
Child 1 → Child 2 → Child 3 → Child 4 → Child 5
To reverse the direction of this line, we cannot simply ask them to let go all at once, or they will lose track of who is where. Instead, we do it child-by-child using a 3-step shuffle:
- Step 1 (Look behind): Before Child 2 lets go of Child 3, they make sure they know who is behind them (we store
Child 3in a temporary variable). - Step 2 (Redirect hand): Child 2 turns around and places their hands on the shoulders of the child **behind them** (Child 1).
- Step 3 (Shift): We move our focus to the next child (Child 3) and repeat the process!
Walkthrough of the Main Method Scenario
Let's trace how the program executes step-by-step from the entry point of the main method:
The program creates five nodes linked together in order:
Node last = new Node(5);
Node lastFirst = new Node(4, last);
Node lastSecond = new Node(3, lastFirst);
Node lastThird = new Node(2, lastSecond);
Node head = new Node(1, lastThird);
This constructs the chain: 1 → 2 → 3 → 4 → 5.
The program prints the original list:
traverseLinkedList(head); // Outputs: 1, 2, 3, 4, 5
The program enters reverseLinkedList(head) and loops through the nodes:
- Initial:
previous = null,current = Node 1. - Loop 1 (Node 1):
- Keep track of next:
temp = Node 2. - Flip link:
Node 1now points tonull(since it is the new tail). - Move labels:
previous = Node 1,current = Node 2.
- Keep track of next:
- Loop 2 (Node 2):
- Keep track of next:
temp = Node 3. - Flip link:
Node 2points toNode 1. - Move labels:
previous = Node 2,current = Node 3.
- Keep track of next:
- Loop 3 to 5: Repeat the flipping. At the end,
previouspoints toNode 5, andcurrentisnull. The list is fully reversed:5 → 4 → 3 → 2 → 1.
The program traverses from the new head (which is Node 5):
traverseLinkedList(last); // Outputs: 5, 4, 3, 2, 1
Java Implementation
Below is the complete Java code demonstrating Node traversal and in-place list reversal:
package io.practise.accolite;
public class ReversingALinkedList {
public static void main(String[] args) {
Node last = new Node(5);
Node lastFirst = new Node(4, last);
Node lastSecond = new Node(3, lastFirst);
Node lastThird = new Node(2, lastSecond);
Node head = new Node(1, lastThird);
traverseLinkedList(head);
reverseLinkedList(head);
traverseLinkedList(last);
}
private static void reverseLinkedList(Node head) {
Node previous = null;
Node current = head;
while (current != null) {
Node temp = current.next;
current.next = previous;
previous = current;
current = temp;
}
}
static void traverseLinkedList(Node head) {
while (head.getNext() != null) {
System.out.println(head.getVal());
head = head.getNext();
}
if (head != null) {
System.out.println(head.getVal());
}
}
}
class Node {
int val;
Node next;
Node(int val) {
this.val = val;
}
Node(int val, Node nextNode) {
this.val = val;
this.next = nextNode;
}
public int getVal() {
return val;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
Conclusion
Reversing a singly linked list in-place requires only O(1) auxiliary space because we do not copy or duplicate the nodes—we simply rearrange their forward links to point backwards, one node at a time!