Skip to content
Decoder

Add Two Numbers II

Medium

Problem

Two non-negative whole numbers are stored as linked lists, one digit per node, with the most significant digit first (the way you write numbers). Add them and return the sum as a linked list in the same order.

Example 1

Input
7
Output
2 -> 4 -> 3 + 5 -> 6 -> 4 → 7 -> 8 -> 0 -> 7
Why
7243 + 564 = 7807

Example 2

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

Example 3

Input
0 + 0
Output
0

The numbers can be far too long to fit in a regular integer. Try not to reverse the input lists.

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.