3. What will be the output of the following code snippet that creates a circular singly linked list with nodes 1, 2, 3 and prints the list once?
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class CircularSinglyLinkedList {
constructor() {
this.head = null;
}
append(data) {
const newNode = new Node(data);
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;
}
}
printList() {
if (!this.head) return '';
let result = '';
let temp = this.head;
do {
result += temp.data + ' -> ';
temp = temp.next;
} while (temp !== this.head);
result += 'null';
return result;
}
}
const list = new CircularSinglyLinkedList();
list.append(1);
list.append(2);
list.append(3);
console.log(list.printList());