
Given the head of a singly linked list, reverse the list in place and return the new head. Each node contains an integer val and a pointer next. If the list is empty or has one node, return it unchanged.
Example 1:
Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Explanation: Reversing the pointers makes the last node the new head.
Example 2:
Input: head = [1, 2]
Output: [2, 1]
Explanation: The second node points to the first after reversal.
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 tail becomes the new head.head = [1, 2]Output[2, 1]WhyThe second node points to the first, and the first node becomes the tail.0 <= number of nodes <= 5000-5000 <= Node.val <= 5000The list is singly linkedThe solution should run in linear timedef reverse_linked_list(head):