At Stripe, a service stores events in a singly linked list. Write a function that reverses the list and returns the new head.
Given the head of a singly linked list, reverse the list in-place so that every node points to its previous node instead of its next node.
head, the first node of a singly linked list, or None for an empty listval and nextExample 1
Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Explanation: Every pointer is reversed, so the tail becomes the new head.
Example 2
Input: head = [7]
Output: [7]
Explanation: A single-node list is already reversed.
Example 3
Input: head = []
Output: []
Explanation: An empty list remains empty.
0 <= number of nodes <= 10^5-10^9 <= node.val <= 10^9O(n) time and O(1) extra space