Maximum Twin Sum of a 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.

30:00

Maximum Twin Sum of a Linked List
medium
Free written solution
Topics
Companies

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][2, 10^5].

  • 1≤Node.val≤1051 \leq \text{Node.val} \leq 10^5

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.

1. find middle2. reverse 2nd half3. sum twins
null50412213

Twin sums checked (0 of 2)

-

best

0

Variables
VariableValue
n4
DepthFunction Call
Stack empty
0/24