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

# Odd Even Linked List Python Solution

> Tested Python solution for LeetCode 328 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 328, Medium. Topics: Linked List. [View on LeetCode](https://leetcode.com/problems/odd-even-linked-list/description/).

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

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

## Problem

Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return *the reordered list*.

The **first** node is considered **odd**, and the **second** node is **even**, and so on.

Note that the relative order inside both the even and odd groups should remain as it was in the input.

You must solve the problem in `O(1)` extra space complexity and `O(n)` time complexity.

### Examples

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

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

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

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

### Constraints

* The number of nodes in the linked list is in the range \[0, 10^4]
* -10^6 \<= Node.val \<= 10^6

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/odd_even_linked_list/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/odd_even_linked_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 through list
    # Space: O(1) - only pointer manipulation
    def odd_even_list(self, head: ListNode[int] | None) -> ListNode[int] | None:
        """
        Group odd-indexed nodes followed by even-indexed nodes.

        Example: [1,2,3,4,5] → [1,3,5,2,4]

        Step 1: Separate odd and even chains
        odd:  1 → 3 → 5 → None
        even: 2 → 4 → None

        Step 2: Connect odd tail to even head
        1 → 3 → 5 → 2 → 4 → None
        """
        if not head or not head.next:
            return head

        odd = head
        even: ListNode[int] | None = head.next
        even_head = even

        while even and even.next:
            odd.next = even.next
            odd = odd.next
            even.next = odd.next
            even = even.next

        odd.next = even_head
        return head
```

## Complexity

| Time                            | Space                            |
| ------------------------------- | -------------------------------- |
| O(n) - single pass through list | O(1) - only pointer manipulation |

## Tags

[Grind](/catalog/grind).
