
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 spacehead = [1, 2, 3, 4, 5]Output[5, 4, 3, 2, 1]WhyThe links are reversed one by one, so the original tail becomes the new head.head = [7]Output[7]WhyA single node has no links to reverse, so the list stays the same.head = []Output[]WhyAn empty list has no nodes, so the function returns `None`.0 <= number of nodes <= 10^5-10^9 <= node.val <= 10^9The input list is singly linkedThe solution should reverse the list in-placedef reverse_linked_list(head):