> ## 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.

# Rotate List Python Solution with Tests

> Tested Python solution for LeetCode 61 with 17 pytest cases. Generate a practice environment with lcpy.

LeetCode 61, Medium. Topics: Linked List, Two Pointers. [View on LeetCode](https://leetcode.com/problems/rotate-list/description/).

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

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

## Problem

Given the `head` of a linked list, rotate the list to the right by `k` places.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg)

```
Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]
```

![Example 2](https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg)

```
Input: head = [0,1,2], k = 4
Output: [2,0,1]
```

### Constraints

* The number of nodes in the list is in the range \[0, 500].
* -100 \<= Node.val \<= 100
* 0 \<= k \<= 2 \* 10^9

## Solution

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

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


class Solution:
    # Time: O(n) — single pass to close ring then cut
    # Space: O(1)
    def rotate_right(self, head: ListNode[int] | None, k: int) -> ListNode[int] | None:
        if not head or not head.next or k == 0:
            return head

        # Count length and close the list into a ring
        tail = head
        length = 1
        while tail.next:
            tail = tail.next
            length += 1
        tail.next = head

        # Effective rotations; new tail is at (length - k % length) steps from head
        k %= length
        steps_to_new_tail = length - k
        new_tail = head
        for _ in range(steps_to_new_tail - 1):
            assert new_tail.next is not None
            new_tail = new_tail.next
        assert new_tail.next is not None
        new_head = new_tail.next
        new_tail.next = None
        return new_head
```

## Complexity

| Time                                      | Space |
| ----------------------------------------- | ----- |
| O(n) — single pass to close ring then cut | O(1)  |

## Tags

[Grind](/catalog/grind), [NeetCode All](/catalog/neetcode).
