2Brute2Best
🔥0

Maximum Twin Sum of a Linked List

Medium·LeetCode #2130 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given the head of a singly linked list with an even number of nodes n.

Node i (counting from 0) and node n − 1 − i are called twins: the first node with the last, the second with the second-to-last, and so on. The twin sum is the sum of their two values.

Return the largest twin sum in the list.

Input

head, the first node of a singly linked list with an even number of nodes.

Output

The maximum twin sum.

Example 1
Input: head = [5, 4, 2, 1]
Output: 6

Twin pairs: 5 + 1 = 6 and 4 + 2 = 6. The largest is 6.

Example 2
Input: head = [4, 2, 2, 3]
Output: 7

Twin pairs: 4 + 3 = 7 and 2 + 2 = 4. The largest is 7.

Example 3
Input: head = [1, 100000]
Output: 100001

Only one pair: 1 + 100000.

Constraints
  • • The number of nodes n is even, with 2 ≤ n ≤ 10^5.
  • • 1 ≤ Node.val ≤ 10^5
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“node i and node n − 1 − i”
→ Mirror pairs: the first half against the second half read backwards.
“singly linked list”
→ Can't read backwards → reverse the second half (or stack the first half).
“n is even”
→ The halves are the same size, so every node has a twin and there is no middle node to skip.
03

Approach Complexity Ladder

Brute Force — copy values to an arrayO(N)O(N)

Copy values into a vector, take max of v[i] + v[n−1−i]

Better — stack the first half while finding the middleO(N)O(N/2)

Push first-half values while slow/fast find the middle, then pop against the second half

Optimal — find the middle, reverse the second half, walk bothO(N)O(1)

Slow/fast to the middle, reverse the back half, add pairs side by side

Brute Force Intuition

Twins are defined by index, and arrays have indexes. Copy all values into a vector, then check vals[i] + vals[n − 1 − i] for i in the first half.

Recognition rule for this sub-pattern:

When fast reaches the end, slow is in the middle. Use this to split a list in half.
Remember this · Linked List — Fast & Slow
Trigger
Cycle detection, middle of list, "n-th from end", two lists meeting, repeating sequences.
Think
If one pointer moves faster or starts earlier, where will they meet?
Key question
What speed or gap makes the meeting point tell me the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Linked List — Fast & Slow translate into production architectures and low-level memory mechanics.