Maximum Twin Sum of a Linked List

Try to solve the Maximum Twin Sum of a Linked List problem.

Statement

In a linked list with an even number of nodes (nn), each node at position ii (using 00-based indexing) is paired with the node at position (n1in - 1 - i). These pairs are called twins for all 0i<n/20 ≤ i < n/2.

For example, if n=4n = 4, the twin pairs are:

  • The node at index 00 pairs with the node at index 33.

  • The node at index 11 pairs with the node at index 22.

The twin sum is the sum of a node’s value and its twin’s value. Given the head of a linked list with an even number of nodes, return the maximum twin sum among all pairs.

Constraints:

  • The list contains an even number of nodes in the range [2,103][2, 10^3].

  • 11 \leq Node.value 103\leq 10^3

Examples

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.