#C11232. Add Two Numbers

    ID: 40526 Type: Default 1000ms 256MiB

Add Two Numbers

Add Two Numbers

You are given two numbers, each represented by a linked list where every node contains a single digit. The digits are stored in reverse order (i.e. the ones digit comes first). Your task is to add these two numbers and return the resulting sum as a linked list in the same reverse order.

More formally, if the two numbers are represented as $$L_1 = [a_0, a_1, \dots, a_{n-1}]$$ and $$L_2 = [b_0, b_1, \dots, b_{m-1}],$$ you need to compute their sum and express it as a list $$[s_0, s_1, \dots, s_{k-1}],$$ where at each position: $$ a_i + b_i + \text{carry} = s_i \; (\text{mod } 10)$$ with the carry being forwarded to the next digit.

Note: If one of the lists is empty, it should be considered as 0.

inputFormat

The input consists of two lines:

  1. The first line contains the digits of the first number in reverse order, separated by spaces.
  2. The second line contains the digits of the second number in reverse order, separated by spaces. (This line may be empty, which represents 0.)

outputFormat

Print a single line containing the digits of the sum in reverse order, separated by spaces.## sample

2 4 3
5 6 4
7 0 8