Skip to content
Decoder

Add Two Numbers

Medium

Problem

Two non-negative whole numbers are stored as linked lists, one digit per node, in **reverse order** (the ones digit comes first). Add the numbers and return the sum as a linked list in the same reverse format.

Example 1

Input
2
Output
4 -> 3 + 5 -> 6 -> 4 → 7 -> 0 -> 8
Why
342 + 465 = 807

Example 2

Input
0 + 0
Output
0

Example 3

Input
9
Output
9 + 1 → 0 -> 0 -> 1
Why
99 + 1 = 100

The numbers can be far too long to fit in a regular integer, so add digit by digit.

Constraints

  • 1 <= number of nodes in each list <= 100
  • 0 <= Node.val <= 9
  • Neither number has leading zeros, except the number 0 itself.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.