What is the printed state of the circular linked list after deleting the node with value 3?
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());
Deleting a node removes it from the circle, so the list skips that value.
After deleting node with value 3, the list links 2 directly to 4, so the printed list is 1 -> 2 -> 4 -> (back to head).
What is the printed state of the circular linked list after deleting the head node (value 1)?
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());
Deleting the head moves the head pointer to the next node.
After deleting the head node 1, the head moves to node 2, so the list is 2 -> 3 -> (back to head).
What error will occur when running this code that tries to delete a node from a circular linked list?
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);
Consider what happens if the value to delete is not in the list.
The while loop never breaks if the value is not found, causing an infinite loop.
When deleting the head node in a circular linked list, why must we update the tail node's next pointer?
Think about what keeps the circle intact.
The tail's next pointer must point to the new head to keep the circular structure intact after deleting the old head.
What is the printed state of the circular linked list after deleting the only node (value 10)?
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());
Deleting the only node should leave the list empty.
After deleting the only node, the head becomes null, so printing returns 'null'.
