#C7465. Add Two Numbers Represented by Linked Lists

    ID: 51339 Type: Default 1000ms 256MiB

Add Two Numbers Represented by Linked Lists

Add Two Numbers Represented by Linked Lists

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, meaning the least significant digit comes first. Your task is to add the two numbers and return the sum as a linked list, also in reverse order.

The addition should be performed digit by digit. If the sum of two digits (plus any carry from the previous addition) is greater than or equal to 10, use \(\text{carry} = 1\) for the next digit and store \(\text{sum} \mod 10\) in the current node. Note that the two input lists might have different lengths.

Input Format: Each test case is provided via stdin as two lines. The first line contains space-separated integers representing the first number in reverse order, and the second line contains space-separated integers representing the second number in reverse order.

Output Format: The output is a single line to stdout that contains space-separated integers representing the digits of the sum in reverse order.

inputFormat

The input consists of two lines:

  • The first line contains space-separated integers representing the digits of the first number (in reverse order).
  • The second line contains space-separated integers representing the digits of the second number (in reverse order).

outputFormat

Output a single line containing space-separated integers representing the resulting sum as a linked list in reverse order.

## sample
2 4 3
5 6 4
7 0 8