Doubly linked list Insertion
Submissions: 14987   Accuracy:

46.1%

  Difficulty: Basic   Marks: 1

Given a doubly linked list, a position p and an integer x. The task  is to add a new node with value x at position just after pth node in the doubly linked list.

Input:
First line of input contains number of testcases T. For each testcase, first line of input contains number of nodes in the linked list, and next line contains two integers p and x.

Output:
For each testcase, there will be a single line of output which prints the modified linked list.

User Task:
The task is to complete the function addNode() which head reference, position and data to be inserted as the arguments, with no return type.

Constraints:
1 <= T <= 200
1 <= N <= 103
0 <= p < N

Example:
Input:
2
3
2 4 5
2 6
4
1 2 3 4
0 44

Output:
2 4 5 6
1 44 2 3 4

Explanation:
Testcase 1:
p = 2, and x = 6. So, 6 is inserted after p, i.e, at position 3 (0-based indexing).
 

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Amit Khandelwal, Harshit Sidhwa
Author: Shubham Joshi 1


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.