Skip to content
RED SAGSkills
medium30 ptslinked-list

Add Two Odometer Readings

Two vehicles report their odometer readings as linked lists of decimal digits, given as arrays a and b. The digits are stored least significant first, so [2,4,3] means 342. Neither number has leading zeros, except the number 0 itself ([0]).

Return the sum of the two readings as a list in the same format. The numbers can be far longer than any built-in number type, so add them digit by digit with a carry.

Example 1

Input: a = [2,4,3], b = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2

Input: a = [9,9,9,9,9,9,9], b = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
Explanation: 9999999 + 9999 = 10009998.

Constraints

  • 1 <= a.length, b.length <= 2 * 10^4
  • 0 <= a[i], b[i] <= 9
Hint 1

Walk both lists together, adding the two digits and the carry.

Hint 2

Keep going while either list has digits left or a carry remains.

Discussion 0

Join free to post in the discussion.

Good practice for: Backend Developer, Full Stack Developer, Embedded Systems Engineer, Blockchain Developer, Automation Test Engineer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.