📖 Scenario: You are managing a circular linked list that represents a playlist of songs playing in a loop. Sometimes, you want to remove a song from the playlist by its name.
🎯 Goal: Build a program that creates a circular linked list of songs, sets a target song name to delete, deletes the node with that song name from the circular linked list, and then prints the updated playlist.
📋 What You'll Learn
Create a circular linked list with nodes containing song names: 'SongA', 'SongB', 'SongC', 'SongD'
Create a variable called targetSong with the value 'SongC'
Write a function deleteNode that deletes the node with the song name matching targetSong from the circular linked list
Print the updated circular linked list song names in order, separated by ' -> ', ending with ' -> (back to start)'
💡 Why This Matters
🌍 Real World
Circular linked lists are used in applications like music playlists, where the list loops continuously. Managing such lists by adding or deleting songs is a common real-world task.
💼 Career
Understanding circular linked lists and node deletion is important for software developers working with data structures, embedded systems, or real-time applications where cyclic data flows are common.
Progress0 / 4 steps
1
Create the Circular Linked List
Create a class called Node with a data property of type string and a next property of type Node | null. Then create four nodes with data 'SongA', 'SongB', 'SongC', and 'SongD'. Link them in a circular way so that the last node points back to the first node. Finally, create a variable called head that points to the first node.
DSA Typescript
Hint
Remember to link the last node's next to the first node to make the list circular.
2
Set the Target Song to Delete
Create a variable called targetSong and set it to the string 'SongC'.
DSA Typescript
Hint
Just create a string variable named targetSong with the exact value 'SongC'.
3
Write the Delete Function
Write a function called deleteNode that takes two parameters: head of type Node and target of type string. The function should delete the node with data equal to target from the circular linked list and return the new head of the list. Handle cases where the node to delete is the head, a middle node, or the only node in the list.
DSA Typescript
Hint
Think about three cases: deleting the only node, deleting the head node, and deleting a middle node. Update links carefully to keep the list circular.
4
Print the Updated Circular Linked List
Call deleteNode with head and targetSong and store the result in newHead. Then print the circular linked list starting from newHead by traversing nodes and printing their data separated by ' -> ', ending with ' -> (back to start)'.
DSA Typescript
Hint
Traverse the circular list starting from newHead and print each node's data until you reach newHead again. Use a do-while loop for this.
Practice
(1/5)
1. What happens when you delete the only node in a circular linked list?
easy
A. The list becomes empty (null).
B. The list still contains the deleted node.
C. The list points to itself indefinitely.
D. The list throws an error.
Solution
Step 1: Understand the single node case
When there is only one node, it points to itself forming a circle.
Step 2: Delete the only node
Removing this node leaves no nodes, so the list becomes empty (null).
Final Answer:
The list becomes empty (null). -> Option A
Quick Check:
Single node deleted = empty list [OK]
Hint: Deleting last node empties the list [OK]
Common Mistakes:
Assuming list still has nodes after deleting the only node
Not setting head to null after deletion
2. Which of the following is the correct way to update the previous node's next pointer when deleting a node in a circular linked list?
easy
A. prev.next = node.next;
B. node.next = prev.next;
C. prev = node.next;
D. node = prev.next;
Solution
Step 1: Identify the pointer to update
To remove a node, the previous node's next pointer must skip the node to delete.
Step 2: Update the previous node's next pointer
Set prev.next to node.next to bypass the node being deleted.
Final Answer:
prev.next = node.next; -> Option A
Quick Check:
prev.next points to node.next [OK]
Hint: Prev node's next skips deleted node [OK]
Common Mistakes:
Assigning node.next to prev.next instead of the other way
Changing the node variable instead of prev.next
3. Consider the circular linked list: 10 -> 20 -> 30 -> (back to 10). If we delete the node with value 20, what will be the new list?
medium
A. 10 -> 20 -> 30 -> (back to 10)
B. 10 -> 30 -> (back to 10)
C. 20 -> 30 -> (back to 20)
D. 30 -> 10 -> (back to 30)
Solution
Step 1: Identify node to delete and its previous node
Node with value 20 is between 10 and 30. Previous node is 10.
Step 2: Update previous node's next pointer
Set 10's next pointer to 30, skipping 20.
Final Answer:
10 -> 30 -> (back to 10) -> Option B
Quick Check:
Deleted 20, list links 10 to 30 [OK]
Hint: Link previous node to next of deleted node [OK]
Common Mistakes:
Not updating the previous node's next pointer
Deleting wrong node or not updating links
4. Given this TypeScript code snippet to delete a node from a circular linked list, what is the error?
function deleteNode(head: Node | null, key: number): Node | null {
if (!head) return null;
let curr = head, prev = null;
do {
if (curr.data === key) {
if (prev) prev.next = curr.next;
else head = curr.next;
return head;
}
prev = curr;
curr = curr.next;
} while (curr !== head);
return head;
}
medium
A. Does not handle the empty list case.
B. Uses 'do-while' incorrectly causing infinite loop.
C. Returns null even if node is not found.
D. Does not update the tail node's next pointer when deleting head.
Solution
Step 1: Analyze deletion of head node
If deleting head, code sets head to curr.next but does not update tail's next pointer.
Step 2: Identify missing tail update
In circular list, tail's next must point to new head to maintain circularity.
Final Answer:
Does not update the tail node's next pointer when deleting head. -> Option D
Quick Check:
Tail.next must update on head deletion [OK]
Hint: Update tail's next when head is deleted [OK]
Common Mistakes:
Ignoring tail pointer update after head deletion
Assuming head change auto-updates tail's next
5. You have a circular linked list with nodes: 5 -> 10 -> 15 -> 20 -> (back to 5). You want to delete the node with value 5 (head node). Which steps correctly delete the head and maintain the circular list?
hard
A. Delete head node and set head to null.
B. Set head to head.next only, no other changes.
C. Find tail node, set tail.next to head.next, update head to head.next.
D. Find tail node, set tail.next to null, update head to head.next.
Solution
Step 1: Find the tail node
Traverse list to find node whose next points to head (value 20).
Step 2: Update tail's next pointer and head
Set tail.next to head.next (node with value 10), then update head to head.next.
Final Answer:
Find tail node, set tail.next to head.next, update head to head.next. -> Option C
Quick Check:
Tail.next points to new head after deletion [OK]
Hint: Update tail.next before changing head [OK]
Common Mistakes:
Not updating tail.next causing broken circular link
Setting tail.next to null breaks circular list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.