#2130
Medium Algorithms Maximum twin sum of a linked list
Linked List Two Pointers Stack
81.6% acceptance
Feb 25, 2026
3908
125
In a linked list of size n, where n is even, the ith 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.
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.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn pair_sum(head: Option<Box<ListNode>>) -> i32 {
let mut vals = Vec::new();
let mut cur = &head;
while let Some(node) = cur {
vals.push(node.val);
cur = &node.next;
}
let n = vals.len();
(0..n / 2).map(|i| vals[i] + vals[n - 1 - i]).max().unwrap_or(0)
}
}