What if removing one friend breaks the whole circle? Learn how to keep the circle unbroken!
Why Delete a Node from Circular Linked List in DSA Typescript?
Imagine you have a group of friends sitting in a circle playing a game. If one friend leaves, you need to carefully ask the person before them to link directly to the person after them, so the circle stays unbroken.
Trying to remove a friend manually from the circle is tricky. If you lose track of who is before or after, the circle breaks and the game stops. It's easy to make mistakes and lose friends from the circle unintentionally.
Using a circular linked list, we can easily find the friend to remove and update the links so the circle stays connected without breaking. This keeps the game going smoothly without losing track.
let friends = ['Alice', 'Bob', 'Charlie', 'Diana']; // To remove Bob, manually shift elements or rebuild array friends = friends.filter(name => name !== 'Bob');
class Node { data: string; next: Node | null; constructor(data: string) { this.data = data; this.next = null; } } // Remove node with data 'Bob' by updating links in circular list
This lets us remove any friend from the circle quickly and safely, keeping the circle unbroken and the game running.
In music playlists that loop songs endlessly, deleting a song from the circular list of tracks without stopping the music is like removing a node from a circular linked list.
Manual removal from a circle is error-prone and breaks the chain.
Circular linked lists keep the circle connected by updating links.
Deleting a node updates pointers to maintain the loop smoothly.
Practice
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 AQuick Check:
Single node deleted = empty list [OK]
- Assuming list still has nodes after deleting the only node
- Not setting head to null after deletion
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 AQuick Check:
prev.next points to node.next [OK]
- Assigning node.next to prev.next instead of the other way
- Changing the node variable instead of prev.next
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 BQuick Check:
Deleted 20, list links 10 to 30 [OK]
- Not updating the previous node's next pointer
- Deleting wrong node or not updating links
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;
}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 DQuick Check:
Tail.next must update on head deletion [OK]
- Ignoring tail pointer update after head deletion
- Assuming head change auto-updates tail's 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 CQuick Check:
Tail.next points to new head after deletion [OK]
- Not updating tail.next causing broken circular link
- Setting tail.next to null breaks circular list
Ready to prove it?
Pass the test and this pattern moves unknown
