Skip to the code
cf.
+25 XP

#234 Best Paired Score

medium

A quiz app ranks an even number of players in a linked list, best first. For a team round it pairs players from both ends: the 1st with the last, the 2nd with the second-to-last, and so on. A pair's score is the sum of the two players' points.

Write bestPairScore(head) that returns the highest pair score. Node i (counting from 0) is paired with node n - 1 - i, where n is the number of nodes.

Example 1

Input: head = [5,4,2,1]
Output: 6

The pairs are 5 + 1 = 6 and 4 + 2 = 6, so the best is 6.

Example 2

Input: head = [4,2,2,3]
Output: 7

The pairs are 4 + 3 = 7 and 2 + 2 = 4.

Constraints

  • The list has an even number of nodes, from 2 to 10^5.
  • 1 <= node.val <= 10^5

Lists are written as arrays here, like [1, 2, 3]. Your function works with ListNode objects, each with val and next.

Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).