Find the maximum twin sum in an even-length linked list.
2130. Maximum Twin Sum of a Linked List
gfgYou are given the head of a singly linked list with an even number of nodes.
For each node at position from the start, its twin is the node at position from the start, where is the number of nodes in the list.
A twin sum is the sum of values of a node and its twin. Return the largest twin sum among all pairs.
Input Format
A singly linked list head representing an even-length list of integer values.
Output Format
Return a single integer: the maximum twin sum over all mirrored pairs.
Constraints
• The list contains an even number of nodes. • Node values are integers. • Aim for time. • Extra space should be minimized when possible.
Example 1
Input
head = [5,4,2,1]
Output
6
Explanation
The twin pairs are (5,1) and (4,2). Their sums are 6 and 6, so the maximum twin sum is 6.
Example 2
Input
head = [4,2,2,3]
Output
7
Explanation
The twin pairs are (4,3) and (2,2). Their sums are 7 and 4, so the answer is 7.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.