Add Two Numbers

Linked Lists, problem 6 of 8

Add Two Numbers

Medium

LC #2

dummy headmathcarry

Not attempted yet

Two non-negative integers are stored as linked lists of digits in reverse order: the head holds the ones digit. Return their sum as a list in the same format.

Neither number has leading zeros, except the number 0 itself.

Example 1

Input: l1 = [9,4,2], l2 = [5,6]
Output: [4,1,3]
249 + 65 = 314

Example 2

Input: l1 = [0], l2 = [0]
Output: [0]

Example 3

Input: l1 = [9,9,9], l2 = [1]
Output: [0,0,0,1]
999 + 1 = 1000

Constraints

  • 1 ≤ nodes in each list ≤ 2 · 10⁴
  • 0 ≤ Node.val ≤ 9

Python

Loading draft…

Test results

7 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 4 hidden

Run examples, then submit all tests.