What if you could flip your entire list in just a few pointer swaps, no copying needed?
Why Reverse a Doubly Linked List in DSA Java?
Imagine you have a list of your favorite songs in order, but suddenly you want to play them from last to first. Doing this by hand means rewriting the entire list backwards, which takes time and can cause mistakes.
Manually reversing a list means you must carefully rewrite each item in reverse order. This is slow, easy to mess up, and if the list is long, it becomes a big headache.
Reversing a doubly linked list swaps the direction of the links between nodes, so you can walk through the list backward without rewriting anything. This is fast and safe because it just changes pointers, not the data.
for (int i = list.size() - 1; i >= 0; i--) { System.out.println(list.get(i)); }
Node oldHead = head;
Node current = head;
while (current != null) {
Node temp = current.next;
current.next = current.prev;
current.prev = temp;
current = temp;
}
head = tail;
tail = oldHead;You can quickly reverse the order of elements in a list to access or process them backward without extra memory or copying.
In a music player app, reversing the playlist order lets users switch from playing songs from first-to-last to last-to-first instantly.
Manual reversal is slow and error-prone.
Reversing a doubly linked list swaps links to reverse order efficiently.
This operation enables quick backward traversal without extra space.
Practice
Solution
Step 1: Understand the structure of a doubly linked list
A doubly linked list has nodes with pointers to both previous and next nodes, allowing traversal in both directions.Step 2: Purpose of reversing
Reversing swaps the previous and next pointers of each node, making the last node the first and vice versa.Final Answer:
To change the direction of traversal so the last node becomes the first -> Option AQuick Check:
Reversing means swapping directions [OK]
- Confusing reversing with deleting nodes
- Thinking reversing sorts the list
- Assuming it converts to singly linked list
next and prev pointers of a node curr in a doubly linked list?Solution
Step 1: Identify correct swapping logic
Swapping two pointers requires a temporary variable to hold one pointer before overwriting it.Step 2: Check each option
A temporary variable is required to swap pointers without data loss.curr.next = curr.prev; curr.prev = null;incorrectly setsprevto null.Node temp = curr.prev; curr.prev = curr.next; curr.next = null;uses temp but wrongly setsnextto null.curr.next = curr.prev; curr.prev = curr.next;overwrites without preserving original values.Node temp = curr.next; curr.next = curr.prev; curr.prev = temp;correctly swaps the pointers.Final Answer:
Node temp = curr.next; curr.next = curr.prev; curr.prev = temp; -> Option DQuick Check:
Use temp variable to swap pointers [OK]
- Swapping without a temporary variable
- Setting one pointer to null accidentally
- Swapping pointers in wrong order
class Node {
int data;
Node prev, next;
Node(int d) { data = d; prev = next = null; }
}
// List: 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second; second.prev = head;
second.next = third; third.prev = second;
// Reverse code snippet
Node curr = head;
Node temp = null;
while (curr != null) {
temp = curr.prev;
curr.prev = curr.next;
curr.next = temp;
curr = curr.prev;
}
if (temp != null) head = temp.prev;
// Print list forward
Node ptr = head;
while (ptr != null) {
System.out.print(ptr.data + " ");
ptr = ptr.next;
}Solution
Step 1: Understand reversal logic
The code swapsprevandnextfor each node, then movescurrto the original next node (nowprevafter swap).Step 2: Trace the list after reversal
Original list: 1 <-> 2 <-> 3
After reversal, head points to node with data 3.
Printing from head forward prints: 3 2 1Final Answer:
3 2 1 -> Option BQuick Check:
Reversed list prints nodes backward [OK]
- Not updating head after reversal
- Confusing prev and next pointers
- Printing from old head instead of new head
Node curr = head;
while (curr != null) {
Node temp = curr.next;
curr.next = curr.prev;
curr.prev = temp;
curr = curr.next;
}Solution
Step 1: Analyze pointer swapping
The code swapsnextandprevcorrectly using temp.Step 2: Check loop iteration step
After swapping,curr.nextpoints to the originalprev. To move forward in the original list,currshould move tocurr.prev(which is original next). The code incorrectly moves tocurr.next, causing wrong traversal.Final Answer:
The loop moves curr to curr.next instead of curr.prev after swapping -> Option CQuick Check:
Move curr to curr.prev after swapping [OK]
- Moving curr to wrong pointer after swap
- Confusing temp usage
- Incorrect loop condition
Solution
Step 1: Understand list reversal effect
After reversal, the head points to the original tail. The list is still doubly linked and can be traversed forward.Step 2: Efficient middle node finding
The two-pointer technique (slow and fast pointers) starting from the new head finds the middle in one pass efficiently.Final Answer:
Use two pointers starting from the new head: one moves one step, the other moves two steps until the faster reaches null -> Option AQuick Check:
Two-pointer method finds middle in O(n) [OK]
- Trying to find middle from old head after reversal
- Counting nodes from tail which is inefficient
- Reversing list again unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
