Problem title
Difficulty
Avg time to solve

Next Greater Element
Easy
10 mins
K Most Frequent Words
Moderate
36 mins
Minimum Depth Of Binary Tree
Moderate
20 mins
Best Time to Buy and Sell
Moderate
20 mins
Stack using queue
Moderate
25 mins
Deepest Leaves Sum
Easy
15 mins
Rotate matrix to the right
Moderate
20 mins
Subarray with equal occurrences
Moderate
15 mins
Max GCD Pair
Moderate
35 mins
Leaders in an array
Easy
15 mins
8

Add One to Linked List

Difficulty: EASY
Contributed By
Avg. time to solve
10 min
Success Rate
90%

Problem Statement

Ninja has been given a number that is represented in the form of a linked list such that each digit corresponds to a node. He has been asked to add 1 to it and return the updated list.

For Example:

1234 is represented as (1 -> 2 -> 3 -> 4) and adding 1 to it should change it to (1 -> 2 -> 3 -> 5).

Note:

The input Linked list does not have any trailing zeros.
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains ‘N’ space-separated integers denoting the nodes of the Linked List. Here, ‘-1’ will be present at the end of each test case denoting the termination of the linked list.
Output Format:
For each case, we need to return the updated linked list where each node will be separated by a space. 

The output of each test case will be printed in a separate line.
Note:
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 10000
0 <= |nodeValue| <= 9

Where ‘N’ denotes the size of the linked list.

Time Limit: 1 sec
Sample Input 1:
1 
4 2 9 -1 
Sample Output 1:
4 3 0 -1
Explanation of Sample Input 1:
Test case 1:
For the first test case of sample output 1, our input number is ‘429’. On adding ‘1’ to it, we get output as ‘430’.
Sample Input 2:
2 
9 -1
9 5 6 5 4 3 5 -1
Sample Output 2:
1 0 -1
9 5 6 5 4 3 6 -1
Explanation of Sample Input 2:
Test case 1:
For the first test case of sample output 2, our given input is ‘9’. On adding 1 to it, we get ‘10’ for which we will have to create a new node and the number of digits has increased from ‘1’ to’2’.
Reset Code
Full screen
copy-code
Console