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

# Maximum Twin Sum of a Linked List

> Tested Python solution for LeetCode 2130 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 2130, [Medium](/catalog/medium). Topics: [Linked List](/catalog/topics/linked-list), [Two Pointers](/catalog/topics/two-pointers), [Stack](/catalog/topics/stack). [View on LeetCode](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/description/).

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

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

## Problem

In a linked list of size `n`, where `n` is **even**, the `i^th` node (**0-indexed**) of the linked list is known as the **twin** of the `(n-1-i)^th` node, if `0 <= i <= (n / 2) - 1`.

* For example, if `n = 4`, then node `0` is the twin of node `3`, and node `1` is the twin of node `2`. These are the only nodes with twins for `n = 4`.

The **twin sum** is defined as the sum of a node and its twin.

Given the `head` of a linked list with even length, return *the **maximum twin sum** of the linked list*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/12/03/eg1drawio.png)

```
Input: head = [5,4,2,1]
Output: 6
Explanation:
Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.
There are no other nodes with twins in the linked list.
Thus, the maximum twin sum of the linked list is 6.
```

![Example 2](https://assets.leetcode.com/uploads/2021/12/03/eg2drawio.png)

```
Input: head = [4,2,2,3]
Output: 7
Explanation:
The nodes with twins present in this linked list are:
- Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.
- Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.
Thus, the maximum twin sum of the linked list is max(7, 4) = 7.
```

![Example 3](https://assets.leetcode.com/uploads/2021/12/03/eg3drawio.png)

```
Input: head = [1,100000]
Output: 100001
Explanation:
There is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.
```

### Constraints

* The number of nodes in the list is an **even** integer in the range `[2, 10^5]`.
* `1 <= Node.val <= 10^5`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_twin_sum_of_a_linked_list/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_twin_sum_of_a_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 second half, walk pairs
    # Space: O(1) — in-place pointer reversal
    def pair_sum(self, head: ListNode[int] | None) -> int:
        # Slow/fast pointers: slow lands on the start of the 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 in place
        prev: ListNode[int] | None = None
        current: ListNode[int] | None = slow
        while current:
            nxt = current.next
            current.next = prev
            prev = current
            current = nxt

        # Walk both halves from the ends inward
        best = 0
        first: ListNode[int] | None = head
        second: ListNode[int] | None = prev
        while second:
            assert first is not None
            best = max(best, first.val + second.val)
            first = first.next
            second = second.next
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n) — find middle, reverse second half, walk pairs | O(1) — in-place pointer reversal |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.