algorythms
In-place List Reversal
LC #92Medium

Reverse Linked List II

In-place List Reversal
MetaAmazonMicrosoft

Problem

Reverse the nodes of the list from position left to right in one pass.

linked-list

Constraints

  • 1 ≤ n ≤ 500
  • -500 ≤ Node.val ≤ 500
  • 1 ≤ left ≤ right ≤ n
  • Must be done in one pass

Example

Inputhead = [1, 2, 3, 4, 5], left = 2, right = 4
Output[1, 4, 3, 2, 5]
Why

Nodes at positions 2–4 (1-indexed) are reversed in one pass

Hints — reveal one at a time