Maximum Twin Sum of a Linked List

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

Statement

In a linked list of even length nn, the node at position ii (0-based indexing) is considered the twin of the node at position (n−1−i)(n-1-i) for all 0≤i<n/20 ≤ i < n/2. For example, if n=4n=4, node 00 and node 33 are twins, and node 11 and node 22 are twins. These pairs are the only twins in a linked list of size 44.

The twin sum is defined as 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 of the linked list.

Constraints:

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

  • 1≤1 \leq Node.data ≤103\leq 10^3

Examples

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