
Given the head of a singly linked list, reverse the list in-place and return the new head. Each node contains an integer value and a next pointer. The function should handle an empty list and a single-node list correctly.
Example 1:
Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Explanation: The next pointers are reversed so traversal starts at 5 and ends at 1.
Example 2:
Input: head = [7]
Output: [7]
Explanation: A single-node list is already reversed.
0 <= number of nodes <= 5000-5000 <= node.val <= 5000head = [1, 2, 3, 4, 5]Output[5, 4, 3, 2, 1]WhyEach node's next pointer is reversed, so the list order becomes the opposite of the input.head = [7]Output[7]WhyA list with one node does not change when reversed.0 <= number of nodes <= 5000-5000 <= node.val <= 5000The list is singly linkedReverse the list in-place by updating pointersdef reverse_linked_list(head):