3. What is the output of the following code?
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
function reverseList(head) {
let prev = null;
let current = head;
while (current) {
let nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
return prev;
}
// Create list: 1 -> 2 -> 3 -> null
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
let reversedHead = reverseList(head);
let result = [];
let node = reversedHead;
while (node) {
result.push(node.val);
node = node.next;
}
console.log(result);