Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Example 1
Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]
Explanation: Positions 2 to 4 hold 2, 3, 4. Reversing just that part gives 4, 3, 2, while 1 and 5 stay in place.
Example 2
Input: head = [5], left = 1, right = 1
Output: [5]
Explanation: The range covers only the single node, so nothing changes.
Example 3
Input: head = [1,2,3], left = 1, right = 3
Output: [3,2,1]
Explanation: The range 1 to 3 covers the whole list, so the entire list is reversed.
Anchor a pointer just before position left. Then repeatedly take the node after the current one and splice it to the front of the sublist (right after the anchor). After right-left such moves, the sublist is reversed in place.
Run checks all cases above. Submit evaluates all test cases.