Reverse Linked List II

Given the head of a linked list and positions m and n, reverse the nodes from position m to n and return the head. Example: Input: head = [1,2,3,4,5], m = 2, n = 4 Output: [1,4,3,2,5] Explanation: Nodes from 2 to 4 are reversed. Pattern focus: In-Place Reversal (reverse sublist).

Input Format

head = ListNode, m = int, n = int

Output Format

return ListNode

Constraints

  • The number of nodes is in the range [1, 500].
  • 1 <= m <= n <= length of list

Examples

Example 1:

Input:

head = [1,2,3,4,5]
m = 2
n = 4

Output:

[1,4,3,2,5]

Explanation:

Sublist [2,3,4] is reversed to [4,3,2].

Loading...
Reverse Linked List II - Linked List DSA Problem