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

# Reorder List Python Solution with Tests

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

LeetCode 143, Medium. Topics: Linked List, Two Pointers, Stack, Recursion. [View on LeetCode](https://leetcode.com/problems/reorder-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 143   # by problem number
lcpy gen -s reorder_list   # by problem name
```

## Problem

You are given the head of a singly linked-list. The list can be represented as:

L0 → L1 → … → Ln - 1 → Ln

*Reorder the list to be on the following form:*

L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …

You may not modify the values in the list's nodes. Only nodes themselves may be changed.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg)

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

![Example 2](https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg)

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

### Constraints

* The number of nodes in the list is in the range \[1, 5 \* 10^4].
* 1 \<= Node.val \<= 1000

## Solution

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

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


class Solution:
    # Time: O(n) where n is the number of nodes
    # Space: O(1) - only using constant extra space
    def reorder_list(self, head: ListNode[int] | None) -> None:
        """
        Reorder a linked list in-place: L0→L1→...→Ln-1→Ln becomes L0→Ln→L1→Ln-1→L2→Ln-2→...

        Algorithm:
        1. Find the middle of the list using slow/fast pointers
        2. Reverse the second half of the list
        3. Merge the first half and reversed second half alternately

        This approach uses O(1) space and O(n) time.
        """
        if not head or not head.next:
            return

        # Step 1: Find the middle of the list
        slow = fast = head
        while fast.next and fast.next.next:
            assert slow.next
            slow = slow.next
            fast = fast.next.next

        # Split the list into two halves
        second_half = slow.next
        slow.next = None  # Break the connection

        # Step 2: Reverse the second half
        prev = None
        current = second_half
        while current:
            next_temp = current.next
            current.next = prev
            prev = current
            current = next_temp
        second_half = prev

        # Step 3: Merge the two halves alternately
        first_half = head
        while second_half:
            assert first_half is not None
            # Store next nodes
            first_next = first_half.next
            second_next = second_half.next

            # Reorder: first -> second -> first_next
            first_half.next = second_half
            second_half.next = first_next

            # Move to next nodes
            first_half = first_next
            second_half = second_next
```

## Complexity

| Time                                | Space                                  |
| ----------------------------------- | -------------------------------------- |
| O(n) where n is the number of nodes | O(1) - only using constant extra space |

## Tags

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