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

Last Updated: 22 Apr, 2022

Easy

```
Assume that the Indexing for the linked list starts from 0.
```

```
Input: ‘N’ = 5, 'LIST' = [1, 1, 2, 3, 4, -1], ‘VAL’ = 2, ‘POS’ = 1.
Output: 1 -> 2 -> 1 -> 2 -> 3 -> 4
Here in the given list we can see that the node having value 2 is inserted at position 1.
```

```
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
The first line of each test case contains an integer, ‘N’, denoting the number of nodes in the linked list.
The third line of the test case contains ‘N + 1’ space-separated integers which are the nodes of the linked list and each line ends with -1 to indicate that the sublist is over. Thus, -1 will never be a linked list element.
The fourth line of each test case contains two spaced integers ‘VAL’ and ‘POS’ denoting the value of the node and the position at which the node have to be inserted respectively.
```

```
The first and only line of each test case in the output contains the linked list after inserting a node having the value ‘VAL’ at position ‘POS’ in the linked list.
```

```
You don't need to print anything. It has already been taken care of. Just implement the given function.
```

```
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^4
0 <= ‘POS <= N
1 <= ‘data’, 'VAL' <= 10^7
Where 'N' is the size of the singly linked list, and ‘data’ is the Integer data of the singly linked list.
Time Limit: 1 sec
```

If ‘POS’ is 0 means that we have to add a node at the beginning of the linked list so just make a new node and set its ‘NEXT’ to the head of the linked list and then set the head of the linked list to the new node.

If ‘POS’ is ‘N’ means that we have to add a node at the end of the linked list so just traverse till the end of the linked list and set the ‘NEXT’ of the end node to the new node.

If ‘POS’ is in the range [1, N-1] then we simply traverse till ‘POS - 1’th node and add the new node just after that node, lets denote the node at ‘POS - 1’ as ‘curr’ and the new node that we have to add as ‘newNode’ we have to just set ‘newNode → next = curr → next’ and ‘curr → next = newNode’.

- Make a new node to insert in the list.
- If ‘POS’ is 0
- Set ‘newNode -> next = head’.
- Assign ‘head = newNode’.
- Return.

- Assign ‘temp’ to ‘head’.
- while ‘temp->next’ is not equal to NULL
- ‘n += 1’
- ‘temp = temp -> next’.

- If ‘POS’ is ‘N’
- ‘temp -> next = newNode’.
- Return.

- Assign ‘temp’ to ‘head’.
- Move ‘temp’ at ‘POS - 1’ (say iterator ‘i’)
- ‘temp = temp -> next’.

- ‘newNode -> next = temp -> next’.
- ‘temp -> next = newNode’.

Similar problems

Deletion In Doubly Linked List

Easy

Posted: 22 Apr, 2022

Deletion In Doubly Linked List

Easy

Posted: 22 Apr, 2022

Insertion In Doubly Linked List

Easy

Posted: 24 Apr, 2022

Insertion In Doubly Linked List

Easy

Posted: 24 Apr, 2022

LRU Cache

Moderate

Posted: 10 Sep, 2022

Delete Nodes On Regular Intervals

Ninja

Posted: 19 Dec, 2022

Add One To Linked List

Hard

Posted: 19 Dec, 2022