Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Delete a Node from Circular Linked List in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting a node from circular linked list

What is the printed state of the circular linked list after deleting the node with value 3?

DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class CircularLinkedList {
  head: Node | null = null;

  append(value: number) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      newNode.next = this.head;
      return;
    }
    let current = this.head;
    while (current.next !== this.head) {
      current = current.next!;
    }
    current.next = newNode;
    newNode.next = this.head;
  }

  delete(value: number) {
    if (!this.head) return;
    let current = this.head;
    let prev: Node | null = null;
    do {
      if (current.value === value) {
        if (prev) {
          prev.next = current.next;
          if (current === this.head) {
            this.head = current.next;
          }
        } else {
          // Deleting head node
          let tail = this.head;
          while (tail.next !== this.head) {
            tail = tail.next!;
          }
          if (this.head === this.head.next) {
            this.head = null;
          } else {
            tail.next = this.head.next;
            this.head = this.head.next;
          }
        }
        return;
      }
      prev = current;
      current = current.next!;
    } while (current !== this.head);
  }

  print(): string {
    if (!this.head) return "null";
    let result = "";
    let current = this.head;
    do {
      result += current.value + " -> ";
      current = current.next!;
    } while (current !== this.head);
    result += "(back to head)";
    return result;
  }
}

const cll = new CircularLinkedList();
cll.append(1);
cll.append(2);
cll.append(3);
cll.append(4);
cll.delete(3);
console.log(cll.print());
A1 -> 2 -> 4 -> (back to head)
B1 -> 2 -> 3 -> 4 -> (back to head)
C1 -> 2 -> (back to head)
D2 -> 3 -> 4 -> (back to head)
Attempts:
2 left
💡 Hint

Deleting a node removes it from the circle, so the list skips that value.

❓ Predict Output
intermediate
2:00remaining
Output after deleting the head node in circular linked list

What is the printed state of the circular linked list after deleting the head node (value 1)?

DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class CircularLinkedList {
  head: Node | null = null;

  append(value: number) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      newNode.next = this.head;
      return;
    }
    let current = this.head;
    while (current.next !== this.head) {
      current = current.next!;
    }
    current.next = newNode;
    newNode.next = this.head;
  }

  delete(value: number) {
    if (!this.head) return;
    let current = this.head;
    let prev: Node | null = null;
    do {
      if (current.value === value) {
        if (prev) {
          prev.next = current.next;
          if (current === this.head) {
            this.head = current.next;
          }
        } else {
          // Deleting head node
          let tail = this.head;
          while (tail.next !== this.head) {
            tail = tail.next!;
          }
          if (this.head === this.head.next) {
            this.head = null;
          } else {
            tail.next = this.head.next;
            this.head = this.head.next;
          }
        }
        return;
      }
      prev = current;
      current = current.next!;
    } while (current !== this.head);
  }

  print(): string {
    if (!this.head) return "null";
    let result = "";
    let current = this.head;
    do {
      result += current.value + " -> ";
      current = current.next!;
    } while (current !== this.head);
    result += "(back to head)";
    return result;
  }
}

const cll = new CircularLinkedList();
cll.append(1);
cll.append(2);
cll.append(3);
cll.delete(1);
console.log(cll.print());
A3 -> 2 -> (back to head)
B2 -> 3 -> (back to head)
C1 -> 2 -> 3 -> (back to head)
Dnull
Attempts:
2 left
💡 Hint

Deleting the head moves the head pointer to the next node.

🔧 Debug
advanced
2:00remaining
Identify the error in circular linked list deletion

What error will occur when running this code that tries to delete a node from a circular linked list?

DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class CircularLinkedList {
  head: Node | null = null;

  delete(value: number) {
    if (!this.head) return;
    let current = this.head;
    let prev: Node | null = null;
    while (current.value !== value) {
      prev = current;
      current = current.next!;
    }
    if (prev) {
      prev.next = current.next;
    } else {
      this.head = current.next;
    }
  }
}

const cll = new CircularLinkedList();
cll.head = new Node(1);
cll.head.next = cll.head;
cll.delete(2);
AReferenceError: current is not defined
BTypeError: Cannot read property 'value' of null
CNo error, deletion succeeds
DInfinite loop
Attempts:
2 left
💡 Hint

Consider what happens if the value to delete is not in the list.

🧠 Conceptual
advanced
1:30remaining
Why update tail's next pointer when deleting head in circular linked list?

When deleting the head node in a circular linked list, why must we update the tail node's next pointer?

ATo convert the list into a singly linked list
BTo free memory used by the tail node
CTo maintain the circular link so the list remains connected
DTo reverse the list order after deletion
Attempts:
2 left
💡 Hint

Think about what keeps the circle intact.

❓ Predict Output
expert
2:00remaining
Output after deleting the only node in circular linked list

What is the printed state of the circular linked list after deleting the only node (value 10)?

DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class CircularLinkedList {
  head: Node | null = null;

  append(value: number) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      newNode.next = this.head;
      return;
    }
    let current = this.head;
    while (current.next !== this.head) {
      current = current.next!;
    }
    current.next = newNode;
    newNode.next = this.head;
  }

  delete(value: number) {
    if (!this.head) return;
    let current = this.head;
    let prev: Node | null = null;
    do {
      if (current.value === value) {
        if (prev) {
          prev.next = current.next;
          if (current === this.head) {
            this.head = current.next;
          }
        } else {
          // Deleting head node
          let tail = this.head;
          while (tail.next !== this.head) {
            tail = tail.next!;
          }
          if (this.head === this.head.next) {
            this.head = null;
          } else {
            tail.next = this.head.next;
            this.head = this.head.next;
          }
        }
        return;
      }
      prev = current;
      current = current.next!;
    } while (current !== this.head);
  }

  print(): string {
    if (!this.head) return "null";
    let result = "";
    let current = this.head;
    do {
      result += current.value + " -> ";
      current = current.next!;
    } while (current !== this.head);
    result += "(back to head)";
    return result;
  }
}

const cll = new CircularLinkedList();
cll.append(10);
cll.delete(10);
console.log(cll.print());
Anull
B10 -> (back to head)
CError: Cannot read property 'next' of null
DEmpty list
Attempts:
2 left
💡 Hint

Deleting the only node should leave the list empty.

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