Insertion Node in a Doubly Linked List

Last Updated : 18 Aug, 2026

Given the head of a doubly-linked list, a position p, and an integer x. Insert a new node with value x at the position just after pth node (0-based indexing) in the doubly linked list and return the head of the modified list.

Examples:

Input: p = 2, x = 6

1

Output: 2 <-> 4 <-> 5 <-> 6
Explanation: Insert a node of value 6 after the 2nd node.

2

Input: p = 0, x = 44

3

Output: 1 <-> 44 <-> 2 <-> 3 <-> 4
Explanation: Insert a node of value 44 after the 0th node.

4
Try It Yourself
redirect icon

[Naive Approach] Using Auxiliary Array - O(n) Time and O(n) Space

The idea is to first store all the nodes of the doubly linked list in an array. This allows direct access to the p-th node, after which the new node is inserted by updating the required next and prev pointers.

Working of Approach:

  • Traverse the list and store each node in an array.
  • Access the p-th node directly from the array.
  • Create a new node with value x.
  • Update the next and prev pointers to insert the new node.
  • Return the head of the modified list.
C++
#include <iostream>
#include <vector>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;
    Node *prev;

    Node(int x)
    {
        data = x;
        next = prev = nullptr;
    }
};

Node *insertAtPos(Node *head, int p, int x)
{

    // Store all nodes in an array
    vector<Node *> nodes;
    Node *curr = head;

    while (curr)
    {
        nodes.push_back(curr);
        curr = curr->next;
    }

    // Create new node
    Node *newNode = new Node(x);

    // Get p-th node
    Node *pNode = nodes[p];

    // Insert new node
    newNode->next = pNode->next;
    newNode->prev = pNode;

    if (pNode->next)
        pNode->next->prev = newNode;

    pNode->next = newNode;

    return head;
}

// Function to print the doubly linked list
void printList(Node *head)
{
    while (head)
    {
        cout << head->data;
        if (head->next)
            cout << " <-> ";
        head = head->next;
    }
    cout << endl;
}

int main()
{

    // Creating linked list: 2 <-> 4 <-> 5
    Node *head = new Node(2);
    head->next = new Node(4);
    head->next->prev = head;
    head->next->next = new Node(5);
    head->next->next->prev = head->next;

    int p = 2, x = 6;

    head = insertAtPos(head, p, x);

    printList(head);

    return 0;
}
Java
import java.util.ArrayList;

class Node {
    public int data;
    public Node next;
    public Node prev;

    public Node(int x)
    {
        data = x;
        next = prev = null;
    }
}

public class GFG {

    public static Node insertAtPos(Node head, int p, int x)
    {

        // Store all nodes in an array
        ArrayList<Node> nodes = new ArrayList<>();
        Node curr = head;

        while (curr != null) {
            nodes.add(curr);
            curr = curr.next;
        }

        // Create new node
        Node newNode = new Node(x);

        // Get p-th node
        Node pNode = nodes.get(p);

        // Insert new node
        newNode.next = pNode.next;
        newNode.prev = pNode;

        if (pNode.next != null)
            pNode.next.prev = newNode;

        pNode.next = newNode;

        return head;
    }

    // Function to print the doubly linked list
    public static void printList(Node head)
    {
        while (head != null) {
            System.out.print(head.data);
            if (head.next != null)
                System.out.print(" <-> ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args)
    {

        // Creating linked list: 2 <-> 4 <-> 5
        Node head = new Node(2);
        head.next = new Node(4);
        head.next.prev = head;
        head.next.next = new Node(5);
        head.next.next.prev = head.next;

        int p = 2, x = 6;

        head = insertAtPos(head, p, x);

        printList(head);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None
        self.prev = None


def insertAtPos(head, p, x):

    # Store all nodes in an array
    nodes = []
    curr = head

    while curr is not None:
        nodes.append(curr)
        curr = curr.next

    # Create new node
    newNode = Node(x)

    # Get p-th node
    pNode = nodes[p]

    # Insert new node
    newNode.next = pNode.next
    newNode.prev = pNode

    if pNode.next is not None:
        pNode.next.prev = newNode

    pNode.next = newNode

    return head

# Function to print the doubly linked list


def printList(head):
    while head is not None:
        print(head.data, end='')
        if head.next is not None:
            print(' <-> ', end='')
        head = head.next
    print()


if __name__ == '__main__':

    # Creating linked list: 2 <-> 4 <-> 5
    head = Node(2)
    head.next = Node(4)
    head.next.prev = head
    head.next.next = Node(5)
    head.next.next.prev = head.next

    p = 2
    x = 6

    head = insertAtPos(head, p, x)

    printList(head)
C#
using System;
using System.Collections.Generic;

public class Node {
    public int data;
    public Node next;
    public Node prev;

    public Node(int x)
    {
        data = x;
        next = prev = null;
    }
}

public class GFG {
    public static Node insertAtPos(Node head, int p, int x)
    {
        // Store all nodes in an array
        List<Node> nodes = new List<Node>();
        Node curr = head;

        while (curr != null) {
            nodes.Add(curr);
            curr = curr.next;
        }

        // Create new node
        Node newNode = new Node(x);

        // Get p-th node
        Node pNode = nodes[p];

        // Insert new node
        newNode.next = pNode.next;
        newNode.prev = pNode;

        if (pNode.next != null)
            pNode.next.prev = newNode;

        pNode.next = newNode;

        return head;
    }

    // Function to print the doubly linked list
    public static void printList(Node head)
    {
        while (head != null) {
            Console.Write(head.data);
            if (head.next != null)
                Console.Write(" <-> ");
            head = head.next;
        }
        Console.WriteLine();
    }

    public static void Main()
    {
        // Creating linked list: 2 <-> 4 <-> 5
        Node head = new Node(2);
        head.next = new Node(4);
        head.next.prev = head;
        head.next.next = new Node(5);
        head.next.next.prev = head.next;

        int p = 2, x = 6;

        head = insertAtPos(head, p, x);

        printList(head);
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
        this.prev = null;
    }
}

function insertAtPos(head, p, x)
{

    // Store all nodes in an array
    let nodes = [];
    let curr = head;

    while (curr !== null) {
        nodes.push(curr);
        curr = curr.next;
    }

    // Create new node
    let newNode = new Node(x);

    // Get p-th node
    let pNode = nodes[p];

    // Insert new node
    newNode.next = pNode.next;
    newNode.prev = pNode;

    if (pNode.next !== null)
        pNode.next.prev = newNode;

    pNode.next = newNode;

    return head;
}

// Function to print the doubly linked list
function printList(head)
{
    let current = head;
    while (current !== null) {
        process.stdout.write(current.data.toString());
        if (current.next !== null)
            process.stdout.write(" <-> ");
        current = current.next;
    }
    console.log();
}

// Driver Code
let head = new Node(2);
head.next = new Node(4);
head.next.prev = head;
head.next.next = new Node(5);
head.next.next.prev = head.next;

let p = 2, x = 6;

head = insertAtPos(head, p, x);

printList(head);

Output
2 <-> 4 <-> 5 <-> 6

[Expected Approach] Single Traversal - O(n) Time and O(1) Space

The idea is to traverse the doubly linked list until the p-th node is reached. Then create a new node and insert it after the current node by updating the required next and prev pointers.

Working of Approach:

  • Traverse the list until the p-th node is reached.
  • Create a new node with value x.
  • If the current node is the last node, append the new node.
  • Otherwise, insert the new node between the current node and the next node by updating both next and prev pointers.
  • Return the head of the modified doubly linked list.

Let us understand with an example:
Input: p = 2, x = 6

1
  • Traverse the list and stop at the 2nd node, which contains value 5.
  • Create a new node with value 6.
  • Since 5 is the last node, connect 5->next to the new node and set 6->prev to 5.
  • The new node is inserted after the 2nd node while maintaining the doubly linked list links.
  • Output: 2 <-> 4 <-> 5 <-> 6
2
C++
#include <iostream>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;
    Node *prev;

    Node(int x)
    {
        data = x;
        next = prev = nullptr;
    }
};

Node *insertAtPos(Node *head, int p, int x)
{
    Node *newnode = new Node(x);
    Node *cur = head;

    // traverse to the given position
    for (int i = 0; i < p; i++)
        cur = cur->next;

    if (cur->next == nullptr)
    {
        cur->next = newnode;
        newnode->prev = cur;
    }
    else
    {
        newnode->next = cur->next;
        cur->next = newnode;
        newnode->next->prev = newnode;
        newnode->prev = cur;
    }
    return head;
}

// Function to print the doubly linked list
void printList(Node *head)
{
    while (head)
    {
        cout << head->data;
        if (head->next)
            cout << " <-> ";
        head = head->next;
    }
    cout << endl;
}

int main()
{

    // Creating linked list: 2 <-> 4 <-> 5
    Node *head = new Node(2);
    head->next = new Node(4);
    head->next->prev = head;
    head->next->next = new Node(5);
    head->next->next->prev = head->next;

    int p = 2, x = 6;

    head = insertAtPos(head, p, x);

    printList(head);

    return 0;
}
Java
class Node {
    public int data;
    public Node next;
    public Node prev;

    public Node(int x)
    {
        data = x;
        next = prev = null;
    }
}

public class GFG {
    public static Node insertAtPos(Node head, int p, int x)
    {
        Node newnode = new Node(x);
        Node cur = head;

        // traverse to the given position
        for (int i = 0; i < p; i++)
            cur = cur.next;

        if (cur.next == null) {
            cur.next = newnode;
            newnode.prev = cur;
        }
        else {
            newnode.next = cur.next;
            cur.next = newnode;
            newnode.next.prev = newnode;
            newnode.prev = cur;
        }
        return head;
    }

    // Function to print the doubly linked list
    public static void printList(Node head)
    {
        while (head != null) {
            System.out.print(head.data);
            if (head.next != null)
                System.out.print(" <-> ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args)
    {

        // Creating linked list: 2 <-> 4 <-> 5
        Node head = new Node(2);
        head.next = new Node(4);
        head.next.prev = head;
        head.next.next = new Node(5);
        head.next.next.prev = head.next;

        int p = 2, x = 6;

        head = insertAtPos(head, p, x);

        printList(head);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None
        self.prev = None


def insertAtPos(head, p, x):
    newnode = Node(x)
    cur = head

    # traverse to the given position
    for _ in range(p):
        cur = cur.next

    if cur.next is None:
        cur.next = newnode
        newnode.prev = cur
    else:
        newnode.next = cur.next
        cur.next = newnode
        newnode.next.prev = newnode
        newnode.prev = cur
    return head

# Function to print the doubly linked list


def printList(head):
    while head is not None:
        print(head.data, end="")
        if head.next is not None:
            print(" <-> ", end="")
        head = head.next
    print()


if __name__ == '__main__':

    # Creating linked list: 2 <-> 4 <-> 5
    head = Node(2)
    head.next = Node(4)
    head.next.prev = head
    head.next.next = Node(5)
    head.next.next.prev = head.next

    p = 2
    x = 6

    head = insertAtPos(head, p, x)

    printList(head)
C#
using System;

class Node {
    public int data;
    public Node next;
    public Node prev;

    public Node(int x)
    {
        data = x;
        next = prev = null;
    }
}

class GFG {
    public static Node insertAtPos(Node head, int p, int x)
    {
        Node newnode = new Node(x);
        Node cur = head;

        // traverse to the given position
        for (int i = 0; i < p; i++)
            cur = cur.next;

        if (cur.next == null) {
            cur.next = newnode;
            newnode.prev = cur;
        }
        else {
            newnode.next = cur.next;
            cur.next = newnode;
            newnode.next.prev = newnode;
            newnode.prev = cur;
        }
        return head;
    }

    // Function to print the doubly linked list
    public static void printList(Node head)
    {
        while (head != null) {
            Console.Write(head.data);
            if (head.next != null)
                Console.Write(" <-> ");
            head = head.next;
        }
        Console.WriteLine();
    }

    static void Main(string[] args)
    {

        // Creating linked list: 2 <-> 4 <-> 5
        Node head = new Node(2);
        head.next = new Node(4);
        head.next.prev = head;
        head.next.next = new Node(5);
        head.next.next.prev = head.next;

        int p = 2, x = 6;

        head = insertAtPos(head, p, x);

        printList(head);
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
        this.prev = null;
    }
}

function insertAtPos(head, p, x)
{
    let newnode = new Node(x);
    let cur = head;

    // traverse to the given position
    for (let i = 0; i < p; i++)
        cur = cur.next;

    if (cur.next === null) {
        cur.next = newnode;
        newnode.prev = cur;
    }
    else {
        newnode.next = cur.next;
        cur.next = newnode;
        newnode.next.prev = newnode;
        newnode.prev = cur;
    }
    return head;
}

// Function to print the doubly linked list
function printList(head)
{
    let output = "";
    while (head !== null) {
        output += head.data;
        if (head.next !== null)
            output += " <-> ";
        head = head.next;
    }
    console.log(output);
}

// Driver Code
let head = new Node(2);
head.next = new Node(4);
head.next.prev = head;
head.next.next = new Node(5);
head.next.next.prev = head.next;

let p = 2, x = 6;

head = insertAtPos(head, p, x);

printList(head);

Output
2 <-> 4 <-> 5 <-> 6
Comment