3. Given the following code, what will be the output after inserting nodes 10 and 20 into an empty doubly linked list?
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
}
insert(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
newNode.prev = current;
}
print() {
let current = this.head;
let result = '';
while (current) {
result += current.data + ' <-> ';
current = current.next;
}
result += 'null';
console.log(result);
}
}
const list = new DoublyLinkedList();
list.insert(10);
list.insert(20);
list.print();