3. What will be the output of the following TypeScript code that reverses a doubly linked list?
class Node {
data: number;
next: Node | null = null;
prev: Node | null = null;
constructor(data: number) { this.data = data; }
}
function reverse(head: Node | null): Node | null {
let current = head;
let temp: Node | null = null;
while (current !== null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp !== null) {
head = temp.prev;
}
return head;
}
// Create list: 1 <-> 2 <-> 3
const n1 = new Node(1);
const n2 = new Node(2);
n1.next = n2; n2.prev = n1;
const n3 = new Node(3);
n2.next = n3; n3.prev = n2;
const newHead = reverse(n1);
// Print reversed list
let result = [];
let node = newHead;
while (node !== null) {
result.push(node.data);
node = node.next;
}
console.log(result);