📖 Scenario: You are working on a music playlist app. The playlist is stored as a doubly linked list where each node holds a song name. Sometimes, users want to reverse the order of songs to listen backward.
🎯 Goal: Build a doubly linked list with given songs, then reverse the list by swapping the links, and finally print the reversed list.
📋 What You'll Learn
Create a doubly linked list with exact songs: 'Song1', 'Song2', 'Song3', 'Song4'
Create a variable called head pointing to the first node
Write a function called reverseDoublyLinkedList that reverses the list by swapping next and prev pointers
Print the reversed list starting from the new head using console.log
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers (history), and undo-redo features where moving forward and backward is needed.
💼 Career
Understanding linked lists and how to reverse them is a common interview question and helps in mastering pointer manipulation and data structure fundamentals.
Progress0 / 4 steps
1
Create the doubly linked list nodes
Create a class called Node with properties data (string), next (Node or null), and prev (Node or null). Then create four nodes with data values 'Song1', 'Song2', 'Song3', and 'Song4'. Link them to form a doubly linked list where node1 is the head.
DSA Typescript
Hint
Remember to set both next and prev pointers for each node to link them properly.
2
Set the head of the list
Create a variable called head and assign it to node1, which is the first node of the doubly linked list.
DSA Typescript
Hint
The head variable points to the first node in the list.
3
Write the function to reverse the doubly linked list
Write a function called reverseDoublyLinkedList that takes head of type Node | null and returns the new head after reversing. Inside the function, use a while loop to swap the next and prev pointers of each node. Update head to the last node processed and return it.
DSA Typescript
Hint
Swap next and prev for each node. Move to the original next node using the updated pointers.
4
Print the reversed doubly linked list
Call reverseDoublyLinkedList with head and store the result in reversedHead. Then use a while loop to print the data of each node starting from reversedHead, separated by ' -> '. End with 'null' using console.log.
DSA Typescript
Hint
Traverse from the new head and join each node's data with ' -> '. End with 'null'.
Practice
(1/5)
1. What is the main purpose of reversing a doubly linked list?
easy
A. To sort the nodes in ascending order
B. To reverse the order of nodes so the last becomes first and vice versa
C. To delete all nodes from the list
D. To convert it into a singly linked list
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: Purpose of reversing
Reversing swaps the direction so the last node becomes the first and the first becomes the last.
Final Answer:
To reverse the order of nodes so the last becomes first and vice versa -> Option B
Quick Check:
Reversing = changing node order [OK]
Hint: Reversing flips the list order, not deleting or sorting [OK]
Common Mistakes:
Confusing reversing with deleting nodes
Thinking reversing sorts the list
Assuming it converts to singly linked list
2. Which of the following TypeScript code snippets correctly swaps the next and prev pointers of a node in a doubly linked list?
easy
A. let temp = node.next; node.next = node.prev; node.prev = temp;
B. node.next = node.next; node.prev = node.prev;
C. node.prev = node.next; node.next = node.prev;
D. let temp = node.prev; node.prev = node.next; node.next = temp.next;
Solution
Step 1: Identify correct swapping logic
Swapping pointers requires a temporary variable to hold one pointer before assignment.
Step 2: Check each option
let temp = node.next; node.next = node.prev; node.prev = temp; correctly uses a temp variable to swap next and prev. Others either assign incorrectly or lose references.
Final Answer:
let temp = node.next; node.next = node.prev; node.prev = temp; -> Option A
Quick Check:
Swap pointers using temp variable [OK]
Hint: Use a temp variable to swap pointers safely [OK]
Common Mistakes:
Assigning pointers without temp causes data loss
Mixing up next and prev assignments
Using incorrect pointer references
3. What will be the output of the following TypeScript code that reverses a doubly linked list?
class Node {
data: number;
next: Node | null = null;
prev: Node | null = null;
constructor(data: number) { this.data = data; }
}
function reverse(head: Node | null): Node | null {
let current = head;
let temp: Node | null = 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;
}
// Create list: 1 <-> 2 <-> 3
const n1 = new Node(1);
const n2 = new Node(2);
n1.next = n2; n2.prev = n1;
const n3 = new Node(3);
n2.next = n3; n3.prev = n2;
const newHead = reverse(n1);
// Print reversed list
let result = [];
let node = newHead;
while (node !== null) {
result.push(node.data);
node = node.next;
}
console.log(result);
medium
A. []
B. [1, 2, 3]
C. [3, 2, 1]
D. [3, 1, 2]
Solution
Step 1: Trace pointer swaps in the loop
Each node swaps its next and prev. The loop moves forward using the old next pointer now in prev.
Step 2: Update head to new first node
After loop, head is updated to last node processed, which is node with data 3.
Final Answer:
[3, 2, 1] -> Option C
Quick Check:
Reversed list order = [3, 2, 1] [OK]
Hint: Reversing swaps pointers and updates head to last node [OK]
Common Mistakes:
Not updating head after reversal
Confusing next and prev in loop
Printing original list order
4. Identify the bug in this TypeScript function that attempts to reverse a doubly linked list:
function reverse(head: Node | null): Node | null {
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 should return current instead of head
B. The pointers next and prev are swapped incorrectly
C. The loop condition should be current.next !== null
D. The function does not update the head to the new first node after reversal
Solution
Step 1: Check pointer swapping logic
The swapping of next and prev is correct using a temp variable.
Step 2: Verify head update after reversal
The function returns original head without updating it to the new first node, so the reversed list head is incorrect.
Final Answer:
The function does not update the head to the new first node after reversal -> Option D
Quick Check:
Head must update to new first node [OK]
Hint: Always update head to last processed node after reversal [OK]
Common Mistakes:
Forgetting to update head after reversal
Returning old head instead of new head
Incorrect loop condition causing infinite loop
5. Given a doubly linked list with nodes containing integer data, how would you modify the reversal function to also count how many nodes were reversed and return both the new head and the count?
hard
A. Modify the function to use an object return type like { head: Node | null, count: number }, increment count inside the loop, and update head after reversal.
B. Add a global variable to count nodes and return only the new head.
C. Count nodes before reversal and return the count without changing the function.
D. Return an array with the reversed list nodes and count without modifying the reversal logic.
Solution
Step 1: Add a counter variable inside the reversal function
Initialize a count variable to zero and increment it each time a node is processed in the reversal loop.
Step 2: Return both new head and count
After reversal, return an object containing the updated head and the count of nodes reversed.
Final Answer:
Modify the function to use an object return type like { head: Node | null, count: number }, increment count inside the loop, and update head after reversal. -> Option A
Quick Check:
Return head and count together after reversal [OK]
Hint: Use an object return type to return multiple values [OK]
Common Mistakes:
Using global variables instead of local counters
Not updating head before returning
Returning count without reversing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.