All problems
0118MediumLinked ListMathRecursion

Odometer Tape Sum

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2Add Two Numbers

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A workshop logs mileage counters onto punched paper tapes. A tape is a one-way chain of cells: every cell holds a single digit and links to the cell punched after it, and the final cell links to nothing. The punch head starts at the smallest place value, so the front cell of a tape carries the ones digit, the cell after it carries the tens digit, and so on.

Because the harness passes plain JSON, each tape arrives as an array of its digits in punch order, ones digit at index 0. You get two of them, left and right. Neither tape is empty, and neither carries useless high-order padding: the last cell of a tape is nonzero unless the whole tape is a single 0.

Return the tape that records the sum of the two counter values, laid out the same way: an array of digits with the ones digit first. Produce it cell by cell while stepping through both chains together and pushing any overflow into the next place. Do not fold either chain into one big number before adding.

Examples

Example 1

Input
left = [6, 8, 4], right = [7, 3]
Output
[3, 2, 5]

The tapes record 486 and 37. Adding the ones cells gives 13, so the answer's front cell is 3 and 1 rides into the tens place, where 8 + 3 + 1 = 12 leaves 2 and another rider, and 4 + 1 = 5 finishes the hundreds place.

Example 2

Input
left = [5, 9, 9], right = [8, 1]
Output
[3, 1, 0, 1]

995 plus 18 is 1013. The overflow survives past the end of both tapes, so the answer needs one more cell than the longer input.

Example 3

Input
left = [0], right = [4, 7]
Output
[4, 7]

A counter sitting at zero is punched as a single 0 cell, and adding it leaves 74 untouched.

Constraints

  • 1 <= left.length <= 100
  • 1 <= right.length <= 100
  • 0 <= left[i] <= 9
  • 0 <= right[i] <= 9
  • Neither tape has high-order padding: its last digit is nonzero unless the tape is exactly [0]

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def sum_odometer_tapes(left: list[int], right: list[int]) -> list[int]:
Java
public int[] sumOdometerTapes(int[] left, int[] right)
September 7
Apply