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
Input
head = [5,4,2,1]Output
6Nodes 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
Input
head = [4,2,2,3]Output
7The 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