📖 Scenario: You are managing a playlist of songs using a doubly linked list. Each song has a unique title. You want to be able to remove a song by its title from the playlist.
🎯 Goal: Build a doubly linked list of songs and write code to delete a song by its title.
📋 What You'll Learn
Create a doubly linked list with 4 songs: 'SongA', 'SongB', 'SongC', 'SongD'
Create a variable called titleToDelete with the value 'SongC'
Write a function deleteByValue that deletes the first node with the given title
Print the playlist titles from head to tail after deletion
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers, and other apps to manage ordered data with easy forward and backward navigation.
💼 Career
Understanding linked lists and how to manipulate them is a key skill for software developers working with data structures and memory management.
Progress0 / 4 steps
1
Create the doubly linked list with 4 songs
Create a class Node with properties title, prev, and next. Then create 4 nodes with titles 'SongA', 'SongB', 'SongC', and 'SongD'. Link them to form a doubly linked list with head pointing to the first node.
DSA Typescript
Hint
Start by defining the Node class with the required properties. Then create each node and link them using prev and next.
2
Create the variable for the title to delete
Create a variable called titleToDelete and set it to the string 'SongC'.
DSA Typescript
Hint
Just create a constant variable named titleToDelete and assign it the string 'SongC'.
3
Write the deleteByValue function
Write a function called deleteByValue that takes head of the list and a title string. It should find the first node with title equal to the given value and remove it from the list by updating the prev and next pointers. Return the possibly new head of the list.
DSA Typescript
Hint
Traverse the list from head. When you find the node with matching title, update the prev and next pointers of neighbors to remove it. If the node is the head, update head. Return the updated head.
4
Print the playlist after deletion
Call deleteByValue with head and titleToDelete. Then print the titles of the songs from the new head to the end, separated by ' -> ', ending with ' -> null'.
DSA Typescript
Hint
Call deleteByValue(head, titleToDelete) and save the result. Then loop from the new head, building a string of titles separated by ' -> ', ending with ' -> null'. Print this string.
Practice
(1/5)
1. What happens when you delete a node by value in a doubly linked list?
easy
A. All nodes are deleted regardless of value
B. The node with the matching value is removed and links are updated
C. Only the first node is deleted no matter the value
D. The list is reversed instead of deleting
Solution
Step 1: Identify the node with the given value
Traverse the list from head to tail to find the node whose value matches the target.
Step 2: Remove the node and update links
Adjust the previous node's next pointer and the next node's previous pointer to exclude the found node.
Final Answer:
The node with the matching value is removed and links are updated -> Option B
Quick Check:
Deleting by value removes matching node [OK]
Hint: Find node by value, then update previous and next links [OK]
Common Mistakes:
Not updating previous node's next pointer
Not updating next node's previous pointer
Deleting all nodes instead of one
2. Which of the following is the correct TypeScript syntax to delete a node by value in a doubly linked list?
easy
A. function deleteNode(value) { /* code */ }
B. removeNode(value: number) => { /* code */ }
C. deleteNode(value: number): void { /* code */ }
D. delete node(value: number) { /* code */ }
Solution
Step 1: Check method declaration syntax in TypeScript class
Methods inside classes use the syntax methodName(params): returnType { body }.
Step 2: Identify correct syntax for deleteNode method
deleteNode(value: number): void { /* code */ } uses correct TypeScript method syntax with type annotation and void return.
Hint: Use methodName(params): returnType { } inside class [OK]
Common Mistakes:
Using arrow function syntax inside class method
Missing type annotations
Using invalid keywords like 'delete node'
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> 40, what is the list after deleting the node with value 30?
medium
A. 10 <-> 20 <-> 40
B. 10 <-> 30 <-> 40
C. 20 <-> 30 <-> 40
D. 10 <-> 20 <-> 30 <-> 40
Solution
Step 1: Locate node with value 30
Traverse the list: 10 -> 20 -> 30 found at third node.
Step 2: Remove node 30 and link 20 to 40
Update 20's next pointer to 40 and 40's previous pointer to 20.
Final Answer:
10 <-> 20 <-> 40 -> Option A
Quick Check:
Deleted 30, list links updated [OK]
Hint: Remove node, link previous and next nodes directly [OK]
Common Mistakes:
Not updating previous node's next pointer
Not updating next node's previous pointer
Deleting wrong node
4. What is wrong with this delete function snippet for doubly linked list?
deleteNode(value: number): void {
let current = this.head;
while (current !== null) {
if (current.value === value) {
current.prev.next = current.next;
current.next.prev = current.prev;
break;
}
current = current.next;
}
}
medium
A. It does not handle deleting the head node correctly
B. It deletes all nodes with the value instead of one
C. It uses wrong variable names
D. It causes infinite loop
Solution
Step 1: Check handling of head node deletion
If current is head, current.prev is null, so current.prev.next causes error.
Step 2: Identify missing head update
Function does not update this.head when deleting the first node, causing broken list.
Final Answer:
It does not handle deleting the head node correctly -> Option A
Quick Check:
Head deletion needs special handling [OK]
Hint: Check if node is head before updating prev pointers [OK]
Common Mistakes:
Not checking if current.prev is null
Not updating head pointer when deleting first node
Assuming all nodes have prev and next
5. You have a doubly linked list: 5 <-> 10 <-> 15 <-> 20 <-> 25. You want to delete the node with value 5, then delete the node with value 25. What is the final list?
hard
A. 15 <-> 20
B. 5 <-> 10 <-> 15 <-> 20
C. 10 <-> 15 <-> 20 <-> 25
D. 10 <-> 15 <-> 20
Solution
Step 1: Delete node with value 5 (head node)
Update head to 10, set 10.prev to null.
Step 2: Delete node with value 25 (tail node)
Update 20.next to null, removing 25.
Final Answer:
10 <-> 15 <-> 20 -> Option D
Quick Check:
Deleted head and tail nodes correctly [OK]
Hint: Update head or tail pointers when deleting first or last nodes [OK]
Common Mistakes:
Not updating head when deleting first node
Not updating tail when deleting last node
Breaking links between middle nodes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.