Reverse a Doubly Linked List in DSA Java - Time & Space Complexity
We want to understand how the time needed to reverse a doubly linked list changes as the list grows.
Specifically, how does the number of steps grow when the list has more nodes?
Analyze the time complexity of the following code snippet.
public void reverse() {
Node current = head;
Node temp = null;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null) {
head = temp.prev;
}
}
This code reverses the links of a doubly linked list by swapping the previous and next pointers of each node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that visits each node once.
- How many times: It runs once for every node in the list, so n times if there are n nodes.
As the list gets longer, the number of steps grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase in a straight line as the list size grows.
Time Complexity: O(n)
This means the time to reverse the list grows directly with the number of nodes.
[X] Wrong: "Reversing a doubly linked list takes more than linear time because of pointer swaps."
[OK] Correct: Each node is visited once and pointers are swapped in constant time, so total time grows linearly.
Understanding this helps you explain how simple pointer changes can reverse a list efficiently, a common question in interviews.
"What if we changed the list to a singly linked list without previous pointers? How would the time complexity change?"
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
