What if you could flip a whole list backward in just a few steps, no matter how long it is?
Why Reverse a Doubly Linked List in DSA Javascript?
Imagine you have a list of your favorite songs written on paper in order. Now, you want to play them in reverse order. If you try to flip through the paper manually from the end to the start, it's slow and confusing.
Manually reversing the list means you have to find the last song, then the second last, and so on. This is slow and easy to mess up, especially if the list is long. You might lose track or accidentally skip songs.
A doubly linked list stores each song with links to both the next and previous songs. Reversing it means swapping these links for every song. This way, you can quickly flip the entire list without losing order or missing any song.
let reversedList = []; for (let i = songs.length - 1; i >= 0; i--) { reversedList.push(songs[i]); }
function reverseDoublyLinkedList(head) {
let current = head;
let temp = null;
while (current) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp) head = temp.prev;
return head;
}This lets you quickly reverse the order of items in a list, making it easy to navigate backward or change directions efficiently.
Think of a photo gallery app where you can swipe forward or backward through pictures. Reversing the doubly linked list of photos lets the app quickly switch the swipe direction without reloading all images.
Manual reversal is slow and error-prone for linked lists.
Reversing a doubly linked list swaps next and previous links for each node.
This operation makes reversing order fast and reliable.
Practice
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 DQuick Check:
Reverse = swap next and prev pointers [OK]
- Thinking reverse means deleting nodes
- Confusing reverse with sorting
- Adding nodes instead of reversing pointers
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 CQuick Check:
Swap with temp variable = correct [OK]
- Swapping without temp loses pointer data
- Assigning both pointers to same value
- Not swapping pointers at all
Head -> 1 <-> 2 <-> 3 <-> null
After reversal, what is the new head node's value?
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 AQuick Check:
New head after reverse = last node = 3 [OK]
- Assuming head stays same after reverse
- Confusing node values order
- Thinking null becomes head
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;
}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 AQuick Check:
Head must update to new first node [OK]
- Not updating head after reversal
- Incorrect loop condition causing infinite loop
- Returning wrong node at end
Head -> 10 <-> 20 <-> 30 <-> 40 <-> null
After reversing the list, what is the correct sequence of nodes starting from the new head?
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 BQuick Check:
Reverse list order = original tail to head [OK]
- Not reversing the order correctly
- Mixing node values in wrong order
- Confusing null placement in list
Ready to prove it?
Pass the test and this pattern moves unknown
