Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Delete a Node from Circular Linked List in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete a Node from Circular Linked List
Start at head
↓
Check if list empty?
Yes→Stop: Nothing to delete
No↓
Traverse nodes to find target
↓
Found node to delete?
No→Stop: Node not found
Yes↓
Adjust pointers to remove node
↓
Update head if needed
↓
Free node memory (conceptual)
↓
Done
Start from head, check if list is empty, traverse to find node, adjust pointers to remove it, update head if needed, then finish.
Execution Sample
DSA Typescript
function deleteNode(head: Node | null, key: number): Node | null {
  if (!head) return null;
  let curr: Node = head, prev: Node | null = null;
  do {
    if (curr.data === key) {
      if (prev === null) {
        // Delete head
        if (curr.next === curr) return null;
        let tail: Node = curr.next!;
        while (tail.next !== curr) {
          tail = tail.next!;
        }
        tail.next = curr.next!;
        head = curr.next!;
        return head;
      } else {
        prev.next = curr.next;
      }
      curr = curr.next!;
      break;
    }
    prev = curr;
    curr = curr.next!;
  } while (curr !== head);
  return head;
}
This code deletes a node with given key from a circular linked list starting at head.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start at head1 -> 2 -> 3 -> 4 -> (back to 1)curr = head (1), prev = null1 -> 2 -> 3 -> 4 -> (circular)
2Check if curr.data == key (3)?1 -> 2 -> 3 -> 4 -> (back to 1)No pointer change1 -> 2 -> 3 -> 4 -> (circular)
3Move prev=curr, curr=curr.next1 -> 2 -> 3 -> 4 -> (back to 1)prev=1, curr=21 -> 2 -> 3 -> 4 -> (circular)
4Check if curr.data == key (3)?1 -> 2 -> 3 -> 4 -> (back to 1)No pointer change1 -> 2 -> 3 -> 4 -> (circular)
5Move prev=curr, curr=curr.next1 -> 2 -> 3 -> 4 -> (back to 1)prev=2, curr=31 -> 2 -> 3 -> 4 -> (circular)
6Found node to delete (3)1 -> 2 -> 3 -> 4 -> (back to 1)prev.next = curr.next (2.next = 4)1 -> 2 -> 4 -> (back to 1)
7Check if node to delete is head1 -> 2 -> 4 -> (back to 1)No (head remains 1)1 -> 2 -> 4 -> (circular)
8Delete complete1 -> 2 -> 4 -> (back to 1)curr removed1 -> 2 -> 4 -> (circular)
💡 Node with key 3 found and deleted; list updated to exclude node 3.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6Final
curr1 (head)2344
prevnull1222
head11111
List Size44433
Key Moments - 3 Insights
Why do we use a do-while loop instead of a while loop?
Because the list is circular, we must check the head node first before stopping. The do-while ensures we check all nodes including head once (see execution_table steps 2 and 4).
What happens if the node to delete is the head?
We update the head pointer to the next node after deletion to keep the list accessible. In this example, the deleted node is not head, so head stays same (step 7).
How do pointers change to remove the node?
We set prev.next to curr.next, skipping the node to delete. This removes it from the circle (step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'curr' after step 5?
A4
B3
C2
D1
💡 Hint
Check the 'curr' variable in variable_tracker after step 5.
At which step does the pointer 'prev.next' get updated to remove the node?
AStep 4
BStep 7
CStep 6
DStep 3
💡 Hint
Look at the 'Pointer Changes' column in execution_table.
If the node to delete was the head, what would happen to the 'head' pointer?
AIt would be updated to the next node
BIt would be set to null
CIt would remain unchanged
DThe list would become empty
💡 Hint
Refer to key_moments explanation about head update (step 7).
Concept Snapshot
Delete Node from Circular Linked List:
- Start at head, traverse nodes using do-while
- If node data matches key, adjust prev.next to skip node
- If deleting head, update head pointer
- Stop when full circle checked or node deleted
- List remains circular after deletion
Full Transcript
To delete a node from a circular linked list, start at the head node. Use a do-while loop to traverse all nodes because the list loops back to head. For each node, check if its data matches the key to delete. If found, adjust the previous node's next pointer to skip the current node, effectively removing it. If the node to delete is the head, update the head pointer to the next node to keep the list accessible. Continue until the node is deleted or the list is fully traversed. The list remains circular after deletion.

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

  1. Step 1: Understand the single node case

    When there is only one node, it points to itself forming a circle.
  2. Step 2: Delete the only node

    Removing this node leaves no nodes, so the list becomes empty (null).
  3. Final Answer:

    The list becomes empty (null). -> Option A
  4. 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

  1. Step 1: Identify the pointer to update

    To remove a node, the previous node's next pointer must skip the node to delete.
  2. Step 2: Update the previous node's next pointer

    Set prev.next to node.next to bypass the node being deleted.
  3. Final Answer:

    prev.next = node.next; -> Option A
  4. 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

  1. Step 1: Identify node to delete and its previous node

    Node with value 20 is between 10 and 30. Previous node is 10.
  2. Step 2: Update previous node's next pointer

    Set 10's next pointer to 30, skipping 20.
  3. Final Answer:

    10 -> 30 -> (back to 10) -> Option B
  4. 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

  1. Step 1: Analyze deletion of head node

    If deleting head, code sets head to curr.next but does not update tail's next pointer.
  2. Step 2: Identify missing tail update

    In circular list, tail's next must point to new head to maintain circularity.
  3. Final Answer:

    Does not update the tail node's next pointer when deleting head. -> Option D
  4. 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

  1. Step 1: Find the tail node

    Traverse list to find node whose next points to head (value 20).
  2. 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.
  3. Final Answer:

    Find tail node, set tail.next to head.next, update head to head.next. -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard