📖 Scenario: You are managing a playlist of songs using a doubly linked list. Each song is a node with a title and links to the previous and next songs. You want to remove the first song from the playlist when you finish listening to it.
🎯 Goal: Build a doubly linked list with three songs, then delete the first song from the list, and finally print the updated playlist from start to end.
📋 What You'll Learn
Create a doubly linked list with exactly three nodes containing the song titles: 'Song1', 'Song2', and 'Song3'.
Create a variable called head that points to the first node of the list.
Write code to delete the first node from the doubly linked list by updating head and the links.
Print the titles of the songs in the updated list from beginning to end separated by ' -> ' and ending with ' -> null'.
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, web browsers, and other apps to move forward and backward through items efficiently.
💼 Career
Understanding linked list operations like deletion is important for software development roles involving data structures and memory management.
Progress0 / 4 steps
1
Create the Doubly Linked List with Three Songs
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 where song1 is the first node, song2 is the second, and song3 is the third.
DSA Typescript
Hint
Start by defining the Node class with the required properties. Then create three nodes and link their prev and next pointers to form the list.
2
Set the Head of the List
Create a variable called head and set it to point to the first node song1.
DSA Typescript
Hint
Simply assign head to the first node song1.
3
Delete the First Node from the List
Delete the first node from the doubly linked list by updating head to point to the second node. Also, set the prev property of the new head to null to disconnect it from the old first node.
DSA Typescript
Hint
Move head to the next node and set its prev to null to remove the first node.
4
Print the Updated List
Use a while loop to print the titles of the songs starting from head. Print each title followed by ' -> '. After the last node, print 'null'.
DSA Typescript
Hint
Use a loop to go through each node starting from head. Add each title and ' -> ' to a string. After the loop, add 'null' and print the string.
Practice
(1/5)
1. What happens to the head pointer when you delete the first node from a doubly linked list?
easy
A. It moves to the previous node
B. It becomes null immediately
C. It stays pointing to the deleted node
D. It moves to the next node in the list
Solution
Step 1: Understand the role of head pointer
The head pointer always points to the first node in the list.
Step 2: Effect of deleting the first node
When the first node is deleted, head must point to the next node to keep the list accessible.
Final Answer:
It moves to the next node in the list -> Option D
Quick Check:
head points to next node after deletion [OK]
Hint: Head always points to the first valid node [OK]
Common Mistakes:
Setting head to null without checking if list is empty
Not updating head after deletion
Pointing head to deleted node
2. Which of the following is the correct TypeScript syntax to delete the first node from a doubly linked list assuming head is the pointer to the first node?
easy
A. head = head.next; head.prev = null;
B. head.prev = null; head = head.next;
C. head.next = null; head = head.prev;
D. head = null; head.next = null;
Solution
Step 1: Move head to next node
Assign head to head.next to remove first node reference.
Step 2: Set new head's prev to null
Set head.prev to null to disconnect backward link from old first node.
Final Answer:
head = head.next; head.prev = null; -> Option A
Quick Check:
Correct syntax to update head and prev pointer [OK]
Hint: Update head first, then fix prev pointer [OK]
Common Mistakes:
Setting head.prev before moving head
Assigning head.prev to null before head changes
Setting head.next to null incorrectly
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> null, what will be the list after deleting the first node?
medium
A. 20 <-> 30 <-> null
B. 10 <-> 30 <-> null
C. 30 <-> null
D. null
Solution
Step 1: Identify the first node
The first node is 10, which will be removed.
Step 2: Update head to next node
Head moves to 20, and 20.prev is set to null.
Final Answer:
20 <-> 30 <-> null -> Option A
Quick Check:
First node removed, list starts at 20 [OK]
Hint: After deletion, list starts from second node [OK]
Common Mistakes:
Not updating prev pointer of new head
Removing wrong node
Leaving old head in list
4. Consider this TypeScript code snippet to delete the first node from a doubly linked list:
if (head !== null) {
head = head.next;
head.prev = null;
}
What error can occur here?
medium
A. Logical error: head.prev should not be set to null
B. Syntax error due to missing semicolon
C. Runtime error if list has only one node
D. No error, code is correct
Solution
Step 1: Check condition when list has one node
If list has one node, head.next is null after deletion.
Step 2: Accessing head.prev causes error
head becomes null, so head.prev causes runtime error (cannot access property of null).
Final Answer:
Runtime error if list has only one node -> Option C
Quick Check:
Null pointer access error possible [OK]
Hint: Check if head is null before accessing head.prev [OK]
Common Mistakes:
Not checking if head is null after moving to next
Assuming list always has multiple nodes
Ignoring runtime null errors
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> 35 <-> null. Write the correct sequence of operations to delete the first node safely, considering the list might have only one node.
hard
A. head.prev = null; head = head.next;
B. if (head !== null) { head = head.next; if (head !== null) head.prev = null; }
C. if (head.next !== null) { head = head.next; head.prev = null; }
D. head = head.next; head.prev = null;
Solution
Step 1: Check if list is empty
First, verify head is not null to avoid errors on empty list.
Step 2: Move head to next node and check again
After moving head, check if new head is not null before setting prev to null.
Final Answer:
if (head !== null) { head = head.next; if (head !== null) head.prev = null; } -> Option B
Quick Check:
Safe deletion with null checks [OK]
Hint: Always check for null before accessing pointers [OK]
Common Mistakes:
Not checking if head is null after moving
Setting prev pointer without null check
Assuming list has multiple nodes always
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.