In a linked list of even length n, node i (0-indexed) and node n - 1 - i are twins for 0 <= i <= n/2 - 1. The twin sum is 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.
In a linked list of even length n, node i (0-indexed) and node n - 1 - i are twins for 0 <= i <= n/2 - 1. The twin sum is 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:
Input: {"head":[5,4,2,1]}
Output: 6
Constraints:
The number of nodes in the list is an even integer in the range [2,105].
1≤Node.val≤105
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"head":[5,4,2,1]}
The list has 4 nodes, so there are 2 twin pairs: node i pairs with node 4 - 1 - i. Plan: find the middle, reverse the second half, then walk both halves together.