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 gets bigger.
Specifically, how does the work grow when the number of nodes increases?
Analyze the time complexity of the following code snippet.
void 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;
}
}
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: It runs once for every node in the list, so n times if there are n nodes.
As the list grows, 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 work grows in a straight line with the number of nodes.
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 the two pointers per node."
[OK] Correct: Each node is visited once, and swapping pointers is a constant-time operation, so the total time is still linear.
Understanding this helps you explain how pointer changes affect performance and shows you can analyze linked list operations clearly.
"What if we tried to reverse the list recursively instead of iteratively? 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.Step 2: Define reversing operation
Reversing means swapping the direction so the last node becomes the first and vice versa.Final Answer:
To make the last node the first and the first node the last -> Option CQuick Check:
Reverse = last becomes first [OK]
- Thinking reversing deletes nodes
- Confusing reversing with sorting
- Assuming it changes list type
next and prev pointers of a node in C?Solution
Step 1: Use a temporary variable to hold one pointer
Store node->next in temp to avoid losing its value.Step 2: Swap pointers correctly
Assign node->next to node->prev, then node->prev to temp.Final Answer:
temp = node->next; node->next = node->prev; node->prev = temp; -> Option DQuick Check:
Swap pointers using temp variable [OK]
- Swapping without temp loses pointer data
- Assigning both pointers to same value
- Mixing order of assignments
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// List: 1 <-> 2 <-> 3 <-> NULL
struct Node* head = node1; // node1->data = 1
// Reversal code snippet:
struct Node* current = head;
struct 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;
}Solution
Step 1: Trace pointer swaps in the loop
Each node swaps its next and prev pointers, moving current backward through original list.Step 2: Update head after loop
After loop, head is set to temp->prev, which points to original last node (data 3).Final Answer:
3 -> Option BQuick Check:
New head = original tail = 3 [OK]
- Not updating head pointer after reversal
- Confusing prev and next pointers
- Assuming head remains same
void reverse(struct Node** head_ref) {
struct Node* current = *head_ref;
struct Node* temp = NULL;
while (current != NULL) {
temp = current->next;
current->next = current->prev;
current->prev = temp;
current = current->prev;
}
if (temp != NULL) {
*head_ref = temp->prev;
}
}Solution
Step 1: Verify pointer swaps
The next/prev swaps correctly reverse directions as current traverses the original next chain (now in prev).Step 2: Analyze temp assignment
Temp should store current->prev before swapping, not current->next, to avoid losing pointer.Final Answer:
temp should store current->prev instead of current->next -> Option AQuick Check:
Correct temp assignment preserves traversal [OK]
- Incorrect head pointer update logic
- Moving current incorrectly after swap
- Not updating head pointer properly
Solution
Step 1: Understand reversal effect on pointers
Reversing swaps next and prev pointers, so next pointers now point backward in original list.Step 2: Analyze traversal from new head
Starting at new head, following next pointers visits nodes in original reverse order.Final Answer:
Traversing from the new head using next pointers visits nodes in original reverse order -> Option AQuick Check:
Next pointers after reverse = original prev [OK]
- Assuming next pointers still go forward after reverse
- Confusing traversal directions
- Thinking original head remains start
Ready to prove it?
Pass the test and this pattern moves unknown
