Add Two Numbers

Practice this coding question on Coding Gurukul.

Problem statement

Two mechanical counters store decimal numbers as linked digit chains, with the least significant digit first. Their readings can be much longer than a built-in integer, so a controller must add the chains directly. Your task Build the two linked lists, add their represented nonnegative integers digit by digit, and return a result list in the same least-significant-first order. Important details Each list contains at least one digit and has no redundant leading zero at its most significant end, except the number zero. Propagate carry even after one input ends; a final carry needs a new node. Do not convert the whole number to a machine integer. Worked example Chains [7, 1] and [5, 2] represent 17 and 25. Their sum is 42, so the result chain is [2, 4].

Input format

First line: m n. Then m digits of the first list and n digits of the second.

Output format

Print the resulting values in order, separated by spaces. Print EMPTY if there are no values.

Constraints

1 <= m, n <= 100; digits are 0 through 9.

Sample input

2 2
7 1
5 2

Sample output

2 4

Sign in to run or submit a solution in C++, Java or Python.