Practice/Apple/Leetcode 92. Reverse Linked List II
CodingOptional
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 (inclusive), and return the modified list.
The positions are 1-indexed, meaning the first node is at position 1.
left and right[1, 500]-500 <= Node.val <= 5001 <= left <= right <= n where n is the length of the listExample 1:
Input: head = 1 -> 2 -> 3 -> 4 -> 5, left = 2, right = 4 Output: 1 -> 4 -> 3 -> 2 -> 5 Explanation: We reverse the sublist from position 2 to 4, which contains nodes with values 2, 3, and 4
Example 2:
Input: head = 5, left = 1, right = 1 Output: 5 Explanation: Reversing a single node results in the same list
Example 3:
Input: head = 1 -> 2 -> 3, left = 1, right = 3 Output: 3 -> 2 -> 1 Explanation: The entire list is reversed