Maximum Twin Sum
The list has an even number of nodes. The node at index i is the twin of the node at index n - 1 - i, so the first and last are twins, the second and second-to-last are twins, and so on. Return the largest sum of a pair of twins. The list is passed in as head.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
head [5,4,2,1]
returns 6
5+1 = 6 and 4+2 = 6.
Example 2
head [1,100]
returns 101
One pair.
Constraints
2 <= number of nodes <= 100, and the number is even-100 <= Node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Linked lists problems
- Move a Value to the EndMedium
- Odd Even Linked ListMedium
- Partition ListMedium
- Plus One Linked ListMedium
- Remove All Duplicated ValuesMedium
- Remove Nodes With a Larger Node AfterMedium
- Remove Nth Node From End of ListMedium
- Reorder ListMedium