📖 Scenario: You are managing a playlist of songs using a doubly linked list. Each song is a node with a title. You want to remove the last song from the playlist.
🎯 Goal: Build a doubly linked list with three songs, then delete the last song from the list, and finally print the playlist from start to end.
📋 What You'll Learn
Create a doubly linked list with nodes containing song titles: 'Song1', 'Song2', 'Song3'.
Add a variable called head pointing to the first node.
Write a function called deleteFromEnd that removes the last node from the list.
Print the playlist from start to end showing the song titles separated by ' <-> '.
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers, and other apps to move forward and backward through items efficiently.
💼 Career
Understanding linked lists and how to modify them is important for software developers working with data structures and memory management.
Progress0 / 4 steps
1
Create the Doubly Linked List
Create a class called Node with properties title (string), prev (Node or null), and next (Node or null). Then create three nodes with titles 'Song1', 'Song2', and 'Song3'. Link them to form a doubly linked list. Finally, create a variable called head that points to the first node.
DSA Typescript
Hint
Define the Node class with title, prev, and next. Create three nodes and link them by setting next and prev properties. Set head to the first node.
2
Add a Variable for the Current Node
Create a variable called current and set it equal to head. This will help us find the last node.
DSA Typescript
Hint
Set current to head so you can move through the list starting from the first node.
3
Write the Function to Delete from End
Write a function called deleteFromEnd that takes no arguments. Inside, use a while loop with current.next !== null to move current to the last node. Then, if current.prev exists, set current.prev.next to null to remove the last node. If current.prev does not exist, set head to null because the list had only one node.
DSA Typescript
Hint
Use a while loop to reach the last node. Then remove it by changing the previous node's next to null. If the list has only one node, set head to null.
4
Print the Playlist After Deletion
Call the function deleteFromEnd(). Then create a variable called temp and set it to head. Use a while loop to traverse the list from temp. Collect the title of each node in an array called titles. After the loop, print the titles joined by ' <-> '.
DSA Typescript
Hint
Call deleteFromEnd() to remove the last song. Then traverse from head to collect titles. Print them joined by ' <-> '.
Practice
(1/5)
1. What happens to the tail pointer when you delete the last node from a doubly linked list?
easy
A. It becomes null immediately
B. It moves to the previous node of the deleted node
C. It points to the head node
D. It remains unchanged
Solution
Step 1: Understand the tail pointer role
The tail pointer always points to the last node in the list.
Step 2: Effect of deleting the last node
When the last node is deleted, the tail must move to the previous node to remain the last node.
Final Answer:
It moves to the previous node of the deleted node -> Option B
Quick Check:
Tail updates to previous node after deletion [OK]
Hint: Tail always points to last node, so move it back after deletion [OK]
Common Mistakes:
Setting tail to null without checking if list is empty
Not updating tail pointer after deletion
Confusing head and tail pointers
Assuming tail points to head after deletion
2. Which of the following is the correct TypeScript syntax to delete the last node from a doubly linked list assuming tail is not null?
easy
A. tail = tail.prev; tail.next = null;
B. tail = tail.next; tail.prev = null;
C. tail = null; head = null;
D. tail.prev = tail; tail.next = null;
Solution
Step 1: Move tail pointer to previous node
To delete the last node, tail must point to its previous node: tail = tail.prev;
Step 2: Set new tail's next to null
Since tail is now last node, its next must be null: tail.next = null;
Final Answer:
tail = tail.prev; tail.next = null; -> Option A
Quick Check:
Correct syntax moves tail back and nullifies next [OK]
Hint: Tail moves back, then tail.next set to null [OK]
Common Mistakes:
Moving tail forward instead of backward
Not setting tail.next to null
Setting tail to null without updating links
Confusing prev and next pointers
3. Given the doubly linked list: 1 <-> 2 <-> 3 <-> 4 <-> null, what is the list after deleting the last node?
medium
A. 1 <-> 2 <-> 3 <-> null
B. 1 <-> 2 <-> 3 <-> 4 <-> null
C. 2 <-> 3 <-> 4 <-> null
D. 1 <-> 2 <-> null
Solution
Step 1: Identify the last node
The last node is 4, pointed by tail.
Step 2: Delete last node and update tail
Remove node 4, update tail to node 3, set tail.next to null.
Final Answer:
1 <-> 2 <-> 3 <-> null -> Option A
Quick Check:
Last node 4 removed, tail now 3 [OK]
Hint: Remove last node, tail moves to previous node [OK]
Common Mistakes:
Not updating tail pointer
Leaving deleted node linked
Removing wrong node
Confusing head and tail updates
4. Consider this TypeScript code snippet to delete the last node from a doubly linked list: