Given a Doubly Linked List, insert a new node at a specific position in the linked list.
Examples:
Input: Linked List = 1 <-> 2 <-> 4, newData = 3, position = 3
Output: 1 <-> 2 <-> 3 <-> 4
Explanation: New node with data = 3 is inserted at position 3Input: Linked List = 2 <-> 3, newData = 1, position = 1
Output: 1 <-> 2 <-> 3
Explanation: New node with data = 1 is inserted at position 1
Approach:
The idea is to traverse the linked list to find the node at position - 1, say current node. If the position is valid, create a new node with the given data and update its pointers: Set the next pointer of new node to next of current node and previous pointer of new node to current node. Similarly, update next pointer of current node to the new node and prev pointer of new node’s next to the new node.

To insert a new node at a specific position,
- If position = 1, create a new node and make it the head of the linked list and return it.
- Otherwise, traverse the list to reach the node at position – 1, say curr.
- If the position is valid, create a new node with given data, say new_node.
- Update the next pointer of new node to the next of current node and prev pointer of new node to current node, new_node->next = curr->next and new_node->prev = curr.
- Similarly, update next pointer of current node to the new node, curr->next = new_node.
- If the new node is not the last node, update prev pointer of new node’s next to the new node, new_node->next->prev = new_node.
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next, *prev;
Node(int new_data) {
data = new_data;
next = prev = nullptr;
}
};
// Function to insert a new
// node at a given position
Node* insertAtPos(Node* head, int pos, int new_data) {
// Create a new node
Node* new_node = new Node(new_data);
// Insertion at the beginning
if (pos == 1) {
new_node->next = head;
// If the linked list is not empty,
// set the prev of head to new node
if (head != NULL)
head->prev = new_node;
// Set the new node as the head of linked list
head = new_node;
return head;
}
Node* curr = head;
// Traverse the list to find the node before the
// insertion point
for (int i = 1; i < pos - 1 && curr != NULL; ++i) {
curr = curr->next;
}
// If the position is out of bounds
if (curr == NULL) {
delete new_node;
return head;
}
// Set the prev of new node to curr
new_node->prev = curr;
// Set the next of new node to next of curr
new_node->next = curr->next;
// Update the next of current node to new node
curr->next = new_node;
// If the new node is not the last node,
// update prev of next node to new node
if (new_node->next != NULL)
new_node->next->prev = new_node;
// Return the head of the doubly linked list
return head;
}
// Function to print the list in required format
void printList(Node* head) {
Node* curr = head;
while (curr != NULL) {
cout << curr->data;
if (curr->next != NULL) {
cout << " <-> ";
}
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 4
Node* head = new Node(1);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(4);
head->next->next->prev = head->next;
// Insert new node with data 3 at position 3
int data = 3;
int pos = 3;
head = insertAtPos(head, pos, data);
printList(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Define the Node structure
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
// Function to create a new node
struct Node *createNode(int new_data)
{
struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
new_node->prev = NULL;
return new_node;
}
// Function to insert a new node at a given position
struct Node *insertAtPos(struct Node *head, int pos, int new_data)
{
struct Node *new_node = createNode(new_data);
// Insertion at the beginning
if (pos == 1)
{
new_node->next = head;
if (head != NULL)
head->prev = new_node;
head = new_node;
return head;
}
struct Node *curr = head;
// Traverse the list to find the node before insertion point
for (int i = 1; i < pos - 1 && curr != NULL; i++)
{
curr = curr->next;
}
// Position out of bounds
if (curr == NULL)
{
free(new_node);
return head;
}
new_node->prev = curr;
new_node->next = curr->next;
curr->next = new_node;
if (new_node->next != NULL)
new_node->next->prev = new_node;
return head;
}
// Function to print the list in required format
void printList(struct Node *head)
{
struct Node *curr = head;
while (curr != NULL)
{
printf("%d", curr->data);
if (curr->next != NULL)
printf(" <-> ");
curr = curr->next;
}
printf("\n");
}
int main()
{
// Create a hardcoded doubly linked list: 1 <-> 2 <-> 4
struct Node *head = createNode(1);
head->next = createNode(2);
head->next->prev = head;
head->next->next = createNode(4);
head->next->next->prev = head->next;
// Insert new node with data 3 at position 3
int data = 3;
int pos = 3;
head = insertAtPos(head, pos, data);
// Print the updated list
printList(head);
// Free memory
struct Node *temp;
while (head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
return 0;
}
class Node {
int data;
Node next, prev;
Node(int new_data) {
data = new_data;
next = prev = null;
}
}
class GfG {
// Function to insert a new
// node at a given position
static Node insertAtPos(Node head, int pos, int new_data) {
// Create a new node
Node new_node = new Node(new_data);
// Insertion at the beginning
if (pos == 1) {
new_node.next = head;
// If the linked list is not empty,
// set the prev of head to new node
if (head != null)
head.prev = new_node;
// Set the new node as the head of linked list
head = new_node;
return head;
}
Node curr = head;
// Traverse the list to find the node before the
// insertion point
for (int i = 1; i < pos - 1 && curr != null; ++i) {
curr = curr.next;
}
// If the position is out of bounds
if (curr == null) {
return head;
}
// Set the prev of new node to curr
new_node.prev = curr;
// Set the next of new node to next of curr
new_node.next = curr.next;
// Update the next of current node to new node
curr.next = new_node;
// If the new node is not the last node,
// update prev of next node to new node
if (new_node.next != null)
new_node.next.prev = new_node;
// Return the head of the doubly linked list
return head;
}
// Function to print the list in required format
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data);
if (curr.next != null) {
System.out.print(" <-> ");
}
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 4
Node head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(4);
head.next.next.prev = head.next;
// Insert new node with data 3 at position 3
int data = 3;
int pos = 3;
head = insertAtPos(head, pos, data);
printList(head);
}
}
class Node:
def __init__(self, new_data):
self.data = new_data
self.next = None
self.prev = None
# Function to insert a new
# node at a given position
def insertAtPos(head, pos, new_data):
# Create a new node
new_node = Node(new_data)
# Insertion at the beginning
if pos == 1:
new_node.next = head
# If the linked list is not empty,
# set the prev of head to new node
if head is not None:
head.prev = new_node
# Set the new node as the head of linked list
head = new_node
return head
curr = head
# Traverse the list to find the node before the
# insertion point
for i in range(1, pos - 1):
if curr is None:
break
curr = curr.next
# If the position is out of bounds
if curr is None:
return head
# Set the prev of new node to curr
new_node.prev = curr
# Set the next of new node to next of curr
new_node.next = curr.next
# Update the next of current node to new node
curr.next = new_node
# If the new node is not the last node,
# update prev of next node to new node
if new_node.next is not None:
new_node.next.prev = new_node
# Return the head of the doubly linked list
return head
# Function to print the list in required format
def printList(head):
curr = head
while curr is not None:
print(curr.data, end="")
if curr.next is not None:
print(" <-> ", end="")
curr = curr.next
print()
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 4
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(4)
head.next.next.prev = head.next
# Insert new node with data 3 at position 3
data = 3
pos = 3
head = insertAtPos(head, pos, data)
printList(head)
using System;
public class Node {
public int data;
public Node next, prev;
public Node(int new_data) {
data = new_data;
next = null;
prev = null;
}
}
class GfG {
// Function to insert a new
// node at a given position
public static Node insertAtPos(Node head, int pos, int new_data) {
// Create a new node
Node new_node = new Node(new_data);
// Insertion at the beginning
if (pos == 1) {
new_node.next = head;
// If the linked list is not empty,
// set the prev of head to new node
if (head != null)
head.prev = new_node;
// Set the new node as the head of linked list
head = new_node;
return head;
}
Node curr = head;
// Traverse the list to find the node before the
// insertion point
for (int i = 1; i < pos - 1 && curr != null; ++i) {
curr = curr.next;
}
// If the position is out of bounds
if (curr == null) {
return head;
}
// Set the prev of new node to curr
new_node.prev = curr;
// Set the next of new node to next of curr
new_node.next = curr.next;
// Update the next of current node to new node
curr.next = new_node;
// If the new node is not the last node,
// update prev of next node to new node
if (new_node.next != null)
new_node.next.prev = new_node;
// Return the head of the doubly linked list
return head;
}
// Function to print the list in required format
public static void printList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data);
if (curr.next != null) {
Console.Write(" <-> ");
}
curr = curr.next;
}
Console.WriteLine();
}
public static void Main(string[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 4
Node head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(4);
head.next.next.prev = head.next;
// Insert new node with data 3 at position 3
int data = 3;
int pos = 3;
head = insertAtPos(head, pos, data);
printList(head);
}
}
// Definition of a Node in a doubly linked list
class Node {
constructor(new_data) {
this.data = new_data;
this.next = null;
this.prev = null;
}
}
// Function to insert a new
// node at a given position
function insertAtPos(head, pos, new_data) {
// Create a new node
let new_node = new Node(new_data);
// Insertion at the beginning
if (pos === 1) {
new_node.next = head;
// If the linked list is not empty,
// set the prev of head to new node
if (head !== null)
head.prev = new_node;
// Set the new node as the head of linked list
head = new_node;
return head;
}
let curr = head;
// Traverse the list to find the node before the
// insertion point
for (let i = 1; i < pos - 1 && curr !== null; ++i) {
curr = curr.next;
}
// If the position is out of bounds
if (curr === null) {
return head;
}
// Set the prev of new node to curr
new_node.prev = curr;
// Set the next of new node to next of curr
new_node.next = curr.next;
// Update the next of current node to new node
curr.next = new_node;
// If the new node is not the last node,
// update prev of next node to new node
if (new_node.next !== null)
new_node.next.prev = new_node;
// Return the head of the doubly linked list
return head;
}
// Function to print the list in required format
function printList(head) {
let curr = head;
let output = "";
while (curr !== null) {
output += curr.data;
if (curr.next !== null) {
output += " <-> ";
}
curr = curr.next;
}
console.log(output);
}
// Driver Code
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 4
let head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(4);
head.next.next.prev = head.next;
// Insert new node with data 3 at position 3
let data = 3;
let pos = 3;
head = insertAtPos(head, pos, data);
// Print final list
printList(head);
Output
1 <-> 2 <-> 3 <-> 4
Time Complexity: O(n), where n is the number of nodes in doubly linked list
Auxiliary Space: O(1)