> ## 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 Python Solution with Tests

> Tested Python solution for LeetCode 206 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 206, Easy. Topics: Linked List, Recursion. [View on LeetCode](https://leetcode.com/problems/reverse-linked-list/description/).

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

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

## Problem

Given the `head` of a singly linked list, reverse the list, and return the reversed list.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg)

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

![Example 2](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg)

```
Input: head = [1,2]
Output: [2,1]
```

```
Input: head = []
Output: []
```

### Constraints

* The number of nodes in the list is the range `[0, 5000]`.
* `-5000 <= Node.val <= 5000`

**Follow up:** A linked list can be reversed either iteratively or recursively. Could you implement both?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/reverse_linked_list/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/reverse_linked_list/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_list(self, head: ListNode[int] | None) -> ListNode[int] | None:
        if not head:
            return None

        # Iterative approach using three pointers
        # Example: [1,2,3] -> [3,2,1]
        #
        # Initial: prev curr
        #          None  ↓
        #                1 -> 2 -> 3 -> None
        #
        prev: ListNode[int] | None = None
        curr: ListNode[int] | None = head

        while curr:
            # Store next node before breaking the link
            next_node = curr.next
            #
            #         prev curr next_node
            #         None  ↓    ↓
            #               1 -> 2 -> 3 -> None
            #

            # Reverse the current link
            curr.next = prev
            #         None <- 1    2 -> 3 -> None
            #         prev   curr  next_node
            #

            # Move pointers forward
            prev = curr
            curr = next_node
            #                1 <- 2    3 -> None
            #                    prev curr
            #

        #                1 <- 2 <- 3   None
        #                         prev curr
        # prev now points to new head of reversed list
        return prev
```

## Complexity

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

## Tags

[Grind 75](/catalog/grind-75), [Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
