3. Given the following TypeScript code for a circular singly linked list, what will be the output after inserting nodes with values 1, 2, and 3 in order?
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
class CircularList {
head: Node | null = null;
insert(value: number) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
newNode.next = this.head;
} else {
let temp = this.head;
while (temp.next !== this.head) {
temp = temp.next!;
}
temp.next = newNode;
newNode.next = this.head;
}
}
print() {
if (!this.head) return '';
let result = '';
let temp = this.head;
do {
result += temp.value + ' -> ';
temp = temp.next!;
} while (temp !== this.head);
result += 'null';
return result;
}
}
const list = new CircularList();
list.insert(1);
list.insert(2);
list.insert(3);
console.log(list.print());