Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

You are given a non-negative integer represented as a linked list of digits (‘N’ is the number of nodes in the linked list). The digits are stored so that the head of the linked list stores the most significant digit.

Your task is to add one to the integer (which is shown in the form of a linked list) and return the head of the linked list after adding 1.

```
Input: ‘N’ = 3, ‘head’ = [2, 3, 4]
Output: [2, 3, 5]
Explanation: Given number is 234. After adding 1 to it, it becomes 235.
```

Detailed explanation

```
First-line contains 'T', denoting the number of Test cases.
For each Test case:
The first line contains one integer, ‘N’, denoting the number of nodes in the Linked List.
The following line contains ‘N’ integers, denoting the non-negative integer (or the values of nodes of the linked list).
```

```
Return the head of the linked list after adding 1 to the non-negative integer.
```

```
You don't need to print anything. Just implement the given function.
```

```
1 <= T <= 10
1 <= N <= 100
0 <= Node.value <= 9
```

The number in the linked list doesn’t contain any leading zeroes except the number 0, where there’s only one node with the value 0.

```
Time Limit: 1-sec
```

```
2
3
2 3 4
4
1 1 1 9
```

```
2 3 5
1 1 2 0
```

```
Input: ‘N’ = 3, ‘head’ = [2, 3, 4]
Output: [2, 3, 5]
Explanation: Given number is 234. After adding 1 to it, it becomes 235.
For test case 2:
Input: ‘N’ = 4, ‘head’ = [1, 1, 1, 9]
Output: [1, 1, 2, 0]
Explanation: Given number is 1119. After adding 1 to it, it becomes 1120.
```

```
2
4
1 9 9 9
4
9 9 9 9
```

```
2 0 0 0
1 0 0 0 0
```