#C732. Add Two Numbers Represented by Linked Lists

    ID: 51178 Type: Default 1000ms 256MiB

Add Two Numbers Represented by Linked Lists

Add Two Numbers Represented by Linked Lists

You are given two non-negative integers represented as linked lists. The digits are stored in reverse order and each of their nodes contains a single digit. Your task is to add the two numbers and return the sum as a linked list in reverse order.

The addition is performed digit by digit with carry over. In mathematical terms, for two digits \(a\) and \(b\) with a previous carry \(c\), the new digit is \( (a + b + c) \mod 10 \) and the new carry is \(\lfloor (a + b + c)/10 \rfloor\).

For example, if the two linked lists represent the numbers 342 (as 2 → 4 → 3) and 465 (as 5 → 6 → 4), the output should represent 807 (as 7 → 0 → 8).

inputFormat

The input is given via standard input (stdin) as follows:

Line 1: An integer n, the number of nodes in the first linked list. Line 2: n space-separated integers representing the first linked list in reverse order. (If n is 0, this line will be empty.) Line 3: An integer m, the number of nodes in the second linked list. Line 4: m space-separated integers representing the second linked list in reverse order. (If m is 0, this line will be empty.)

outputFormat

Print the resulting linked list that represents the sum. The digits should be printed in reverse order and separated by a single space.## sample

3
2 4 3
3
5 6 4
7 0 8