Loading problem…
You're building a browser navigation feature that needs to reverse the order of visited pages. Given a linked list of page visits, you need to reverse the list to show pages in reverse chronological order (most recent first).
Given the head of a singly linked list, reverse the list and return the new head.
// Input: 1 -> 2 -> 3 -> 4 -> 5 -> null
// Output: 5 -> 4 -> 3 -> 2 -> 1 -> null
const head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
// ... more nodes
const reversed = reverseList(head);
// reversed is now 3 -> 2 -> 1 -> nullThis problem models real-world scenarios: