As reported: Amazon SDE-1 loop (Bangalore, June 2025), round 4 — bar raiser: short introductions and questions about current work, then this coding problem. Edge cases missed at first were pointed out by the interviewer and fixed with hints.
You are given two sorted linked lists l1 and l2 that share some values (common nodes). Build a single list that starts at the head of either list and, at any common value, may switch to the other list, continuing to the end of one of them. Among all such lists, return the one with the maximum sum. Reuse the existing nodes.
The same idea on arrays is LeetCode 1537 — Get the Maximum Score.
Example 1
l1 = 1 → 3 → 30 → 90 → 120 → 240 → 511
l2 = 0 → 3 → 12 → 32 → 90 → 125 → 240 → 249
Output: 1 → 3 → 12 → 32 → 90 → 125 → 240 → 511
Between the common values 3, 90 and 240, take whichever list's stretch has the larger sum.
Example 2
l1 = 2 → 3 → 7 → 10 → 12
l2 = 1 → 5 → 7 → 8
Output: 1 → 5 → 7 → 10 → 12
Constraints: each list has 1 to 10⁵ nodes; values are integers in [0, 10⁶] and strictly increasing within each list.
Clarifications
- A common node appears once in the result.
- If both stretches between two common values (or both tails) have the same sum, take the stretch from
l1.
Code runner format: one JSON value per line: l1, then l2, each as a JSON array of node values. Implement maxSumList(l1, l2) returning the head of the result; the harness prints its values as a JSON array.