#K84377. Add Two Numbers Represented by Linked Lists

    ID: 36406 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, and each node contains a single digit. Your task is to add the two numbers and output the sum as a linked list, where the digits are also stored in reverse order.

The sum is computed using the relation

$$carry = \lfloor \frac{digit1 + digit2 + carry}{10} \rfloor, \quad digit = (digit1 + digit2 + carry) \mod 10, $$

where (digit1) and (digit2) are the current digits from the two linked lists.

Input will be provided from standard input, and your program must output the resulting list to standard output. This problem simulates arithmetic using linked lists.

inputFormat

The input consists of two lines:

  • The first line contains the digits of the first number in reverse order, separated by spaces.
  • The second line contains the digits of the second number in reverse order, separated by spaces. Each list contains at least one digit.

outputFormat

Output the digits of the resultant sum (in reverse order) on a single line, separated by spaces. The output should be written to standard output.## sample

2 4 3
5 6 4
7 0 8