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

# Palindrome Linked List Python Solution

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

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

## Problem

Given the `head` of a singly linked list, return `true` if it is a palindrome or `false` otherwise.

### Examples

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

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

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

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

### Constraints

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

**Follow up:** Could you do it in `O(n)` time and `O(1)` space?

## Solution

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

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


class Solution:
    # Time: O(n) — find middle, reverse half, compare
    # Space: O(1) — in-place pointers
    def is_palindrome(self, head: ListNode[int] | None) -> bool:
        if not head or not head.next:
            return True

        # Slow/fast to reach the middle (slow lands on start of second half)
        slow: ListNode[int] | None = head
        fast: ListNode[int] | None = head
        while fast and fast.next:
            assert slow is not None
            slow = slow.next
            fast = fast.next.next

        # Reverse the second half
        second_head = self._reverse(slow)

        # Compare both halves
        first: ListNode[int] | None = head
        second: ListNode[int] | None = second_head
        result = True
        while second:
            assert first is not None
            if first.val != second.val:
                result = False
                break
            first = first.next
            second = second.next
        return result

    @staticmethod
    def _reverse(head: ListNode[int] | None) -> ListNode[int] | None:
        prev: ListNode[int] | None = None
        current = head
        while current:
            nxt = current.next
            current.next = prev
            prev = current
            current = nxt
        return prev
```

## Complexity

| Time                                      | Space                    |
| ----------------------------------------- | ------------------------ |
| O(n) — find middle, reverse half, compare | O(1) — in-place pointers |

## Tags

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