> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Reverse Linked List II Python Solution

> Tested Python solution for LeetCode 92 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 92, Medium. Topics: Linked List. [View on LeetCode](https://leetcode.com/problems/reverse-linked-list-ii/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 92   # by problem number
lcpy gen -s reverse_linked_list_ii   # by problem name
```

## Problem

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: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]
```

```
Input: head = [5], left = 1, right = 1
Output: [5]
```

### Constraints

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

**Follow up:** Could you do it in one pass?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/reverse_linked_list_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/reverse_linked_list_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import ListNode


class Solution:
    # Time: O(n)
    # Space: O(1)
    def reverse_between(
        self, head: ListNode[int] | None, left: int, right: int
    ) -> ListNode[int] | None:
        if not head or left == right:
            return head

        dummy = ListNode[int](0)
        dummy.next = head
        prev = dummy

        # Move to position before left
        for _ in range(left - 1):
            assert prev.next
            prev = prev.next

        # Reverse from left to right using iterative approach
        # Example: [1,2,3,4,5] left=2, right=4 -> [1,4,3,2,5]
        #
        # Initial: prev curr
        #           ↓    ↓
        #           1 -> 2 -> 3 -> 4 -> 5
        #
        assert prev.next
        curr = prev.next  # First node to be reversed (will become last after reversal)

        # Reverse by moving nodes one by one to the front of the section
        for _ in range(right - left):
            assert curr.next
            next_node = curr.next  # Node to move to front
            #
            #        prev curr next_node
            #          ↓    ↓    ↓
            #          1 -> 2 -> 3 -> 4 -> 5
            #
            curr.next = next_node.next
            #         1 -> 2 -----> 4 -> 5
            #                   3 ↗
            #
            next_node.next = prev.next
            #         1 -> 2 -----> 4 -> 5
            #           3 ↗
            #
            prev.next = next_node
            #         1 -> 3 -> 2 -> 4 -> 5
            #         prev  ↑   curr
            #              next_node

        return dummy.next
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
