What is the printed state of the circular linked list after inserting 10 at the beginning?
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class CircularLinkedList { tail: Node | null = null; insertAtBeginning(value: number) { const newNode = new Node(value); if (!this.tail) { newNode.next = newNode; this.tail = newNode; } else { newNode.next = this.tail.next; this.tail.next = newNode; } } printList(): string { if (!this.tail) return "null"; let result = ""; let current = this.tail.next; do { result += current!.value + " -> "; current = current!.next; } while (current !== this.tail.next); result += "(back to start)"; return result; } } const cll = new CircularLinkedList(); cll.insertAtBeginning(20); cll.insertAtBeginning(30); cll.insertAtBeginning(10); console.log(cll.printList());
Remember, the tail's next points to the head. Inserting at beginning means new node becomes head.
Inserting 10 at beginning adds it after tail and before current head (20). So order is 10 -> 30 -> 20.
When inserting a new node at the beginning of a circular linked list, which pointer should be updated to maintain the circular structure?
Think about which node points to the head in a circular linked list.
The tail's next pointer must point to the new node to keep the circular link intact when inserting at beginning.
What error will occur when running this code snippet for inserting at beginning in a circular linked list?
insertAtBeginning(value: number) {
const newNode = new Node(value);
if (!this.tail) {
newNode.next = null;
this.tail = newNode;
} else {
newNode.next = this.tail.next;
this.tail.next = newNode;
}
}Check what happens when the list is empty and a new node is inserted.
When list is empty, newNode.next must point to itself to maintain circular structure. Setting it to null breaks the list.
What is the printed state of the circular linked list after inserting 5, 15, and 25 at the beginning in this order?
const cll = new CircularLinkedList(); cll.insertAtBeginning(5); cll.insertAtBeginning(15); cll.insertAtBeginning(25); console.log(cll.printList());
Each new insertion at beginning becomes the new head.
Inserting 25 last at beginning makes it the head, followed by 15 and 5.
After inserting 3 nodes at the beginning of an empty circular linked list, what is the length of the list?
Each insertion adds one node to the list.
Inserting 3 nodes adds 3 elements, so length is 3.
