Maximum Twin Sum of a Linked List

In a linked list of size n, where n is even, the i^th 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.

Example 1
[5] -> [4] -> [2] -> [1] -> null
Inputhead = [5,4,2,1]
Output6
Nodes 0 and 1 are the twins of nodes 3 and 2, respectively, and both twin sums are 6, so the maximum twin sum is 6.
Example 2
[4] -> [2] -> [2] -> [3] -> null
Inputhead = [4,2,2,3]
Output7
The twin sums are 4 + 3 = 7 and 2 + 2 = 4, so the maximum twin sum is 7.

Constraints

  • The number of nodes in the list is an even integer in the range [2, 10^5].
  • 1 <= Node.val <= 10^5

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate