Add Two Numbers in Lists

Two non-negative whole numbers are stored in linked lists, one digit per node, with the ones digit first: the list 2 → 4 → 3 is the number 342. Add the two numbers and return the sum as a list in the same form.

Neither number has leading zeros, except the number 0 itself (a single node holding 0).

Lists in the examples are written as their values from head to tail: [1,2,3] is 1 → 2 → 3, and [] is an empty list, with no head at all.

Constraints

  • 1 ≤ number of nodes in each list ≤ 100
  • 0 ≤ node value ≤ 9

Example 1

Input
l1 = [2,4,3]
l2 = [5,6,4]
Output
[7,0,8]
Why:

342 + 465 = 807, written ones first.

Example 2

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

0 + 0 = 0.

Example 3

Input
l1 = [9,9,9,9,9,9,9]
l2 = [9,9,9,9]
Output
[8,9,9,9,0,0,0,1]
Why:

9,999,999 + 9,999 = 10,009,998: the sum has one digit more than the longer number.

Hints

    Was this a good challenge?

    Solved by
    0
    Submissions
    0
    Accepted
    –