Modify Linked List
0% completed
Pair swap
Remove String
Add K Nodes
Add One to Linked List
Append Nodes
1

Add One To Linked List

Difficulty: EASY

Problem Statement
Suggest Edit

Given a non-negative number represented in the form of a singly-linked list of digits. You need to add 1 to the number, i.e., increment the given number by one.

The digits are stored such that the most significant digit is at the starting of the linked list and the least significant digit is at the end of the linked list.

For Example:
If the given list is {1,5,2}, the returned list should be {1,5,3}.
Input Format:
The first line of input contains a single integer T, representing the number of test cases.

The first and the only line of each test case contains space seperated integers denoting the nodes of the linked list. Each line is guaranteed to have -1 at the end to signify the end of the linked list. Also, None of the elements of the linked list is -1.
Output Format:
For each test case, print space-separated integers denoting the nodes of the output linked list.

Each line denotes a new linked list.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <=  10^5
0 <= data <= 9

Where ‘N’ is the number of digits in the given number or number of nodes in the given list, and  ‘data‘ is the value of any node of the linked list.

Time Limit: 1 sec
Sample Input 1:
2
1 2 3 -1
9 9 -1
Sample Output 1:
1 2 4 
1 0 0
Explanation For Sample Input 1:
Test Case 1: The number is 123; after adding 1, the number becomes 124. Hence the output will be {1,2,4}.

Test Case 2: The number is 99; after adding 1, the number becomes 100. Hence the output will be {1,0,0}.
Sample Input 2:
3
2 4 6 8 -1
0 -1
0 2 -1
Sample Output 2:
2 4 6 9 
1
3
Reset Code
Full screen
copy-code
Console