At Stripe, a service stores event sequences as singly linked lists. Write a function that reverses a singly linked list and returns the new head.
You are given the head of a singly linked list. Each node contains a value and a pointer next to the next node, or null if it is the last node. Return the head of the reversed list.
Implement the function so that it updates pointers in-place rather than creating a second list.
Example 1
head = [1, 2, 3, 4, 5][5, 4, 3, 2, 1]next pointer is reversed, so the tail becomes the new head.Example 2
head = [7][7]Example 3
head = [][][0, 5000]-10^4 <= node.val <= 10^4O(n) time and O(1) extra space