All problems

Problem 726

Add One to a Number Represented as a Linked List

Easy · 10 pts · Linked List, Math, Recursion

Problem

A non-negative number is stored in a linked list, one digit per node, with the most significant digit at the head and no leading zeros (except for the number 0 itself). Add 1 to the number and print the digits of the result from head to tail. A carry can ripple through a long run of 9s and even create a new head node. Process the digits from the tail (with recursion or by reversing the list), or find the last digit that is not 9.

Input

The first line contains N, the number of digits. The second line contains the N digits from head to tail.

Output

Print the digits of the result from head to tail, separated by spaces.

Constraints

1 <= N <= 10000; each digit is between 0 and 9; no leading zeros unless the number is 0

Example input

3
1 9 9

Example output

2 0 0

Explanation

The list 1 9 9 stands for 199. Adding one gives 200, so the 9s become 0s and the 1 becomes 2: the answer is 2 0 0.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Flatten a Multilevel Linked List Next: Subset Sums