Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@avaiesshaik782 | 662 |

@arpitsatnalika | 629 |

@code_it_ | 591 |

@prakharxyz | 583 |

@tujhekya | 542 |

@Rahul Jadhav 1 | 458 |

@sivanandh03 | 448 |

@satyam0460 | 429 |

@iamronak | 424 |

@imnm | 424 |

@18eumc107 | 421 |

Complete Leaderboard |

Given a doubly-linked list, a position **p,** and an integer **x.** The task is to add a new node with value **x** at the position just after **p ^{th}** node in the doubly linked list.

**Example 1:**

**Input:
**LinkedList: 2<->4<->5
p = 2, x = 6
**Output: **2 4 5 6**
Explanation: **p = 2, and x = 6. So, 6 is
inserted after p, i.e, at position 3
(0-based indexing).

**Example 2:**

**Input:
**LinkedList: 1<->2<->3<->4
p = 0, x = 44
**Output: **1 44 2 3 4**
Explanation: **p = 0, and x = 44 . So, 44
is inserted after p, i.e, at position 1
(0-based indexing).

**Your 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.

**Expected Time Complexity** : O(N)

**Expected Auxilliary Space** : O(1)

**Constraints:**

1 <= N <= 10^{4}

0 <= p < N

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Doubly linked list Insertion at given position

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.