Reverse a Doubly Linked List in DSA C# - 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 gets longer?
Analyze the time complexity of the following code snippet.
public class Node {
public int data;
public Node prev, next;
}
public Node ReverseDoublyLinkedList(Node head) {
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;
}
return head;
}
This code reverses the links of a doubly linked list so the last node becomes the first.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that visits each node once.
- How many times: Exactly once per node, so n times for 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: Doubling the list size roughly doubles the work needed.
Time Complexity: O(n)
This means the time to reverse the list grows linearly with the number of nodes.
[X] Wrong: "Reversing a doubly linked list takes more than linear time because of the two pointers per node."
[OK] Correct: Each node is visited only once, and swapping pointers is a constant-time step, so the total time still grows linearly.
Understanding this linear time reversal helps you handle linked list problems confidently, a common topic in interviews.
"What if we changed the list to a singly linked list? How would the time complexity of reversal change?"
Practice
Solution
Step 1: Understand the structure of a doubly linked list
A doubly linked list has nodes connected forward and backward using next and prev pointers.Step 2: Purpose of reversing
Reversing swaps the direction so the last node becomes the first and the first becomes the last.Final Answer:
To change the order of nodes so the last becomes first and vice versa -> Option AQuick Check:
Reversing = changing node order [OK]
- Confusing reversing with deleting nodes
- Thinking reversing sorts the list
- Mixing up adding nodes with reversing
next and prev pointers of a node in a doubly linked list?Solution
Step 1: Understand swapping pointers
To swap two pointers, use a temporary variable to hold one value before overwriting it.Step 2: Check each option
The correct snippet saves node.next in temp, assigns node.next = node.prev, then node.prev = temp. Direct assignments without temp cause data loss. The flawed temp usage assigns node.next = node.prev without saving the original node.next first, losing data.Final Answer:
var temp = node.next; node.next = node.prev; node.prev = temp; -> Option DQuick Check:
Use temp variable to swap pointers [OK]
- Swapping without a temporary variable
- Assigning pointers in wrong order
- Confusing next and prev roles
class Node {
public int data;
public Node next, prev;
public Node(int d) { data = d; next = prev = 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 function
Node Reverse(Node head) {
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;
return head;
}
head = Reverse(head);
// Print list forward
Node tempNode = head;
string result = "";
while (tempNode != null) {
result += tempNode.data + " -> ";
tempNode = tempNode.next;
}
result += "null";
Console.WriteLine(result);Solution
Step 1: Trace the reversal loop
Each node swaps its next and prev pointers. The loop moves current to current.prev (which was originally next). After the loop, head is updated to the last processed node.Step 2: Verify the new order
Original list: 1 <-> 2 <-> 3. After reversal, the list becomes 3 <-> 2 <-> 1. Printing forward from new head prints 3 -> 2 -> 1 -> null.Final Answer:
3 -> 2 -> 1 -> null -> Option AQuick Check:
Reversed list prints backward order [OK]
- Not updating head after reversal
- Confusing next and prev pointers
- Printing list without updating head
Node Reverse(Node head) {
Node current = head;
Node temp = null;
while (current != null) {
temp = current.next;
current.next = current.prev;
current.prev = temp;
current = current.next;
}
if (temp != null) head = temp.prev;
return head;
}Solution
Step 1: Analyze pointer swapping
The code swaps next and prev pointers correctly, but moves current to current.next, which after swapping points backward.Step 2: Correct traversal direction
After swapping, current should move to current.prev (originally next) to continue forward traversal. Moving to current.next causes skipping or infinite loop.Final Answer:
The loop should move current to current.prev, not current.next -> Option BQuick Check:
Traverse using swapped pointers correctly [OK]
- Moving current in wrong direction after swap
- Incorrect temp assignment
- Wrong head update after reversal
Solution
Step 1: Initialize a counter before the loop
Start a counter at zero to track how many nodes are processed during reversal.Step 2: Increment counter inside the reversal loop
Each time a node is processed (pointers swapped), increase the counter by one.Step 3: Return both new head and count
Modify the function to return the new head and the count of reversed nodes.Final Answer:
Add a counter variable initialized to 0, increment it inside the reversal loop, and return it along with the new head. -> Option CQuick Check:
Count nodes during reversal loop [OK]
- Counting nodes before reversal only
- Incrementing counter after loop ends
- Not returning the count with the head
Ready to prove it?
Pass the test and this pattern moves unknown
