Skip to content
Decoder

Maximum Twin Sum of a Linked List

Medium

Problem

A linked list has an even number of nodes, n. Node i (0-based) and node n - 1 - i are twins: the first and last are twins, the second and second-to-last are twins, and so on. A twin sum is the sum of a node's value and its twin's value. Return the largest twin sum.

Example 1

Input
5
Output
4 -> 2 -> 1 → 6
Why
5 + 1 = 6, 4 + 2 = 6

Example 2

Input
4
Output
2 -> 2 -> 3 → 7
Why
4 + 3 = 7, 2 + 2 = 4

Example 3

Input
1
Output
100000 → 100001

Constraints

  • 2 <= number of nodes <= 10^5
  • The number of nodes is even.
  • 1 <= Node.val <= 10^5

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.