Skip to content

visual walkthrough

Add Two Numbers

MediumCarry PropagationReported at: AmazonMetaApple+8

Digit-by-digit addition with a carry; very frequently asked.

Solve on LeetCode

The idea

The digits are stored smallest first, which is exactly the order you add in on paper: ones, then tens, then hundreds.

Add the two digits plus the carry, write the last digit of the total, carry the rest. Keep going while either list or the carry has something left.

Complexity

approachtimespace
Read as whole numbersO(n + m)O(n + m)
Digit by digit with a carryO(max(n, m))O(1) extra

More walkthroughs