A doubly linked list is a chain of nodes where each node has three parts: data, a pointer to the next node, and a pointer to the previous node. This allows moving forward and backward through the list.
Click to reveal answer
beginner
What does reversing a doubly linked list mean?
Reversing a doubly linked list means changing the direction of the list so that the last node becomes the first, and the first becomes the last, by swapping the next and previous pointers of each node.
Click to reveal answer
intermediate
In reversing a doubly linked list, what pointers do we swap in each node?
We swap the 'next' pointer with the 'previous' pointer in each node. This changes the direction of the links between nodes.
Click to reveal answer
intermediate
Why do we need to update the head pointer after reversing a doubly linked list?
Because after reversing, the original tail becomes the new head. We must update the head pointer to point to this new first node to correctly represent the reversed list.
Click to reveal answer
beginner
What is the time complexity of reversing a doubly linked list?
The time complexity is O(n), where n is the number of nodes, because we visit each node once to swap its pointers.
Click to reveal answer
What pointers are swapped in each node when reversing a doubly linked list?
ANext and previous pointers
BData and next pointers
COnly next pointers
DOnly previous pointers
✗ Incorrect
Reversing a doubly linked list requires swapping both next and previous pointers in each node.
After reversing a doubly linked list, what should the head pointer point to?
AOriginal tail node
BNull
COriginal head node
DMiddle node
✗ Incorrect
The original tail becomes the new head after reversing the list.
What is the time complexity of reversing a doubly linked list with n nodes?
AO(1)
BO(n^2)
CO(log n)
DO(n)
✗ Incorrect
Each node is visited once to swap pointers, so the time complexity is O(n).
Which of these is NOT a part of a doubly linked list node?
ANext pointer
BRandom pointer
CPrevious pointer
DData
✗ Incorrect
A standard doubly linked list node has data, next, and previous pointers. Random pointer is not standard.
What happens if you forget to update the head pointer after reversing a doubly linked list?
AThe list will be empty
BThe list will still be reversed but head points to old first node
CThe list will be reversed but head points to old first node, causing incorrect traversal
DThe list will be reversed but head points to old tail node
✗ Incorrect
If head is not updated, it points to the old first node which is now the last, causing incorrect traversal.
Explain step-by-step how to reverse a doubly linked list.
Think about swapping pointers and updating head.
You got /4 concepts.
Why is reversing a doubly linked list easier than reversing a singly linked list?
Consider the presence of two pointers per node.
You got /4 concepts.
Practice
(1/5)
1. What does reversing a doubly linked list mean?
easy
A. Adding a new node at the end
B. Deleting all nodes from the list
C. Sorting the nodes in ascending order
D. Swapping the next and previous pointers of each node
Solution
Step 1: Understand doubly linked list structure
Each node has two pointers: one to the next node and one to the previous node.
Step 2: Reverse means swapping pointers
Reversing means changing each node's next pointer to previous and previous pointer to next.
Final Answer:
Swapping the next and previous pointers of each node -> Option D
Quick Check:
Reverse = swap next and prev pointers [OK]
Hint: Reverse means swap next and prev pointers [OK]
Common Mistakes:
Thinking reverse means deleting nodes
Confusing reverse with sorting
Adding nodes instead of reversing pointers
2. Which of the following is the correct way to swap the next and prev pointers of a node in JavaScript?
easy
A. node.prev = node.next; node.next = node.prev;
B. node.next = node.prev; node.prev = node.next;
C. let temp = node.next; node.next = node.prev; node.prev = temp;
D. node.next = node.next; node.prev = node.prev;
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 = node.prev, then node.prev = temp to complete swap.
Final Answer:
let temp = node.next; node.next = node.prev; node.prev = temp; -> Option C
Quick Check:
Swap with temp variable = correct [OK]
Hint: Use temp variable to swap pointers safely [OK]
Common Mistakes:
Swapping without temp loses pointer data
Assigning both pointers to same value
Not swapping pointers at all
3. What will be the output list after reversing this doubly linked list?
Head -> 1 <-> 2 <-> 3 <-> null
After reversal, what is the new head node's value?
medium
A. 3
B. 1
C. 2
D. null
Solution
Step 1: Original list order
List is 1 <-> 2 <-> 3, head points to 1.
Step 2: Reverse swaps pointers, so last node becomes head
After reversal, 3 becomes the new head, pointing backward to 2 and forward to null.
Final Answer:
3 -> Option A
Quick Check:
New head after reverse = last node = 3 [OK]
Hint: New head after reverse is original tail [OK]
Common Mistakes:
Assuming head stays same after reverse
Confusing node values order
Thinking null becomes head
4. Identify the bug in this reverse function for a doubly linked list:
function reverse(head) {
let current = head;
while (current != null) {
let temp = current.next;
current.next = current.prev;
current.prev = temp;
current = current.next;
}
return head;
}
medium
A. The function does not update the head to the new first node
B. Swapping next and prev pointers is done incorrectly
C. The loop condition should be current.next != null
D. The function should return null instead of head
Solution
Step 1: Check pointer swapping
Swapping next and prev pointers is done correctly using temp variable.
Step 2: Check head update after reversal
Function returns original head, but after reversal head should be last node visited, so head is not updated.
Final Answer:
The function does not update the head to the new first node -> Option A
Quick Check:
Head must update to new first node [OK]
Hint: Update head to last processed node after loop [OK]
Common Mistakes:
Not updating head after reversal
Incorrect loop condition causing infinite loop
Returning wrong node at end
5. Given a doubly linked list with nodes:
Head -> 10 <-> 20 <-> 30 <-> 40 <-> null
After reversing the list, what is the correct sequence of nodes starting from the new head?
hard
A. 10 <-> 20 <-> 30 <-> 40 <-> null
B. 40 <-> 30 <-> 20 <-> 10 <-> null
C. 40 <-> 20 <-> 30 <-> 10 <-> null
D. null <-> 40 <-> 30 <-> 20 <-> 10
Solution
Step 1: Original list order
Nodes are linked as 10 <-> 20 <-> 30 <-> 40, head at 10.
Step 2: Reverse swaps pointers and changes head to last node
After reversal, head is 40, and nodes link backward as 40 <-> 30 <-> 20 <-> 10.
Final Answer:
40 <-> 30 <-> 20 <-> 10 <-> null -> Option B
Quick Check:
Reverse list order = original tail to head [OK]
Hint: Reversed list order is original list backwards [OK]
Common Mistakes:
Not reversing the order correctly
Mixing node values in wrong order
Confusing null placement in list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.