85. Reverse Linked List II

MediumLinked List

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.

Examples

Input: [1,2,3,4,5] 2 4

Output: [1,4,3,2,5]

Explanation: The sublist from position 2 to 4 (2,3,4) is reversed.

Constraints

  • The number of nodes in the list is n.
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n
Loading...

Run checks all cases above. Submit evaluates all test cases.