Open In App

Insertion in a Doubly Linked List

Last Updated : 18 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Inserting a new node in a doubly linked list is very similar to inserting new node in linked list. There is a little extra work required to maintain the link of the previous node. A node can be inserted in a Doubly Linked List in four ways:

  • At the front of the DLL. 
  • In between two nodes
    • After a given node.
    • Before a given node.
  • At the end of the DLL.

Insertion at the Beginning in Doubly Linked List:

Insertion_beginning_Doubly-Linked-List

To insert a new node at the beginning of the doubly list, we can use the following steps:

  • Allocate memory for a new node (say new_node) and assign the provided value to its data field.
  • Set the previous pointer of the new_node to nullptr.
  • If the list is empty:
    • Set the next pointer of the new_node to nullptr.
    • Update the head pointer to point to the new_node.
  • If the list is not empty:
    • Set the next pointer of the new_node to the current head.
    • Update the previous pointer of the current head to point to the new_node.
    • Update the head pointer to point to the new_node.

Below is the implementation of the 5 steps to insert a node at the front of the linked list:

C++
// Given a reference (pointer to pointer) to the head 
// of a list and an int, inserts a new node 
// on the front of the list.
void push(Node** head_ref, int new_data)
{
    // 1. allocate node
    Node* new_node = new Node();

    // 2. put in the data
    new_node->data = new_data;

    // 3. Make next of new node as head
    // and previous as NULL
    new_node->next = (*head_ref);
    new_node->prev = NULL;

    // 4. change prev of head node to new node
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;

    // 5. move the head to point to the new node
    (*head_ref) = new_node;
}

// This code is contributed by shivanisinghss2110
C
// Given a reference (pointer to pointer) to the head
// of a list and an int, inserts a new node
// on the front of the list.
void push(struct Node** head_ref, int new_data)
{
    // 1. allocate node
    struct Node* new_node
        = (struct Node*)malloc(sizeof(struct Node));

    // 2. put in the data
    new_node->data = new_data;

    // 3. Make next of new node as head and previous as NULL
    new_node->next = (*head_ref);
    new_node->prev = NULL;

    // 4. change prev of head node to new node
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;

    // 5. move the head to point to the new node
    (*head_ref) = new_node;
}
Java
// Adding a node at the front of the list
public void push(int new_data)
{
    // 1. allocate node
    // 2. put in the data */
    Node new_Node = new Node(new_data);

    // 3. Make next of new node as head and previous as NULL
    new_Node.next = head;
    new_Node.prev = null;

    // 4. change prev of head node to new node
    if (head != null)
        head.prev = new_Node;

    // 5. move the head to point to the new node
    head = new_Node;
}
C#
// Adding a node at the front of the list
public void push(int new_data)
{
    // 1. allocate node
    // 2. put in the data
    Node new_Node = new Node(new_data);

    // 3. Make next of new node as head and previous as NULL
    new_Node.next = head;
    new_Node.prev = null;

    // 4. change prev of head node to new node
    if (head != null)
        head.prev = new_Node;

    // 5. move the head to point to the new node
    head = new_Node;
}

// This code is contributed by aashish1995
Javascript
// Adding a node at the front of the list
function push(new_data)
{
    // 1. allocate node 
    // 2. put in the data
    let new_Node = new Node(new_data);

    // 3. Make next of new node as head and previous as NULL
    new_Node.next = head;
    new_Node.prev = null;

    // 4. change prev of head node to new node
    if (head != null)
        head.prev = new_Node;

    // 5. move the head to point to the new node
    head = new_Node;
}

// This code is contributed by saurabh_jaiswal.
Python3
# Adding a node at the front of the list
def push(self, new_data):

    # 1 & 2: Allocate the Node & Put in the data
    new_node = Node(data=new_data)

    # 3. Make next of new node as head and previous as NULL
    new_node.next = self.head
    new_node.prev = None

    # 4. change prev of head node to new node
    if self.head is not None:
        self.head.prev = new_node

    # 5. move the head to point to the new node
    self.head = new_node

# This code is contributed by jatinreaper

Time Complexity: O(1)
Auxiliary Space: O(1)

Insertion in between two nodes in Doubly Linked List:

Insertion_middle_Doubly-Linked-List

1. Add a node after a given node in a Doubly Linked List:

We are given a pointer to a node as prev_node, and the new node is inserted after the given node. This can be done using the following steps:

  • Firstly create a new node (say new_node).
  • Now insert the data in the new node.
  • Point the next of new_node to the next of prev_node.
  • Point the next of prev_node to new_node.
  • Point the previous of new_node to prev_node.
  • Point the previous of next of new_node to new_node.

Below is the implementation of the steps to insert a node after a given node in the linked list:

C++
// Given a node as prev_node, insert a new node 
// after the given node
void insertAfter(Node* prev_node, int new_data)
{
    // Check if the given prev_node is NULL
    if (prev_node == NULL) {
        cout << "the given previous node cannot be NULL";
        return;
    }

    // 1. allocate new node
    Node* new_node = new Node();

    // 2. put in the data
    new_node->data = new_data;

    // 3. Make next of new node as next of prev_node
    new_node->next = prev_node->next;

    // 4. Make the next of prev_node as new_node
    prev_node->next = new_node;

    // 5. Make prev_node as previous of new_node
    new_node->prev = prev_node;

    // 6. Change previous of new_node's next node
    if (new_node->next != NULL)
        new_node->next->prev = new_node;
}
C
// Given a node as prev_node, insert a new node 
// after the given node
void insertAfter(struct Node* prev_node, int new_data)
{
    // Check if the given prev_node is NULL
    if (prev_node == NULL) {
        printf("the given previous node cannot be NULL");
        return;
    }

    // 1. allocate new node
    struct Node* new_node
        = (struct Node*)malloc(sizeof(struct Node));

    // 2. put in the data
    new_node->data = new_data;

    // 3. Make next of new node as next of prev_node
    new_node->next = prev_node->next;

    // 4. Make the next of prev_node as new_node
    prev_node->next = new_node;

    // 5. Make prev_node as previous of new_node
    new_node->prev = prev_node;

    // 6. Change previous of new_node's next node
    if (new_node->next != NULL)
        new_node->next->prev = new_node;
}
Java
// Given a node as prev_node, insert a new node 
// after the given node
public void InsertAfter(Node prev_Node, int new_data)
{
    // Check if the given prev_node is NULL
    if (prev_Node == null) {
        System.out.println(
            "The given previous node cannot be NULL ");
        return;
    }

    // 1. allocate node
    // 2. put in the data 
    Node new_node = new Node(new_data);

    // 3. Make next of new node as next of prev_node
    new_node.next = prev_Node.next;

    // 4. Make the next of prev_node as new_node
    prev_Node.next = new_node;

    // 5. Make prev_node as previous of new_node
    new_node.prev = prev_Node;

    // 6. Change previous of new_node's next node
    if (new_node.next != null)
        new_node.next.prev = new_node;
}
C#
// Given a node as prev_node, insert a new node 
// after the given node
public void InsertAfter(Node prev_Node, int new_data)
{
    // Check if the given prev_node is NULL
    if (prev_Node == null) {
        Console.WriteLine(
            "The given previous node cannot be NULL ");
        return;
    }

    // 1. allocate node
    // 2. put in the data
    Node new_node = new Node(new_data);

    // 3. Make next of new node as next of prev_node
    new_node.next = prev_Node.next;

    // 4. Make the next of prev_node as new_node
    prev_Node.next = new_node;

    // 5. Make prev_node as previous of new_node
    new_node.prev = prev_Node;

    // 6. Change previous of new_node's next node
    if (new_node.next != null)
        new_node.next.prev = new_node;
}

// This code is contributed by aashish1995
Javascript
<script>

function InsertAfter(prev_Node,new_data)
{
        // Check if the given prev_node is NULL
    if (prev_Node == null) {
        document.write("The given previous node cannot be NULL <br>");
        return;
    }

    // 1. allocate node 
    // 2. put in the data
    let new_node = new Node(new_data);

    // 3. Make next of new node as next of prev_node
    new_node.next = prev_Node.next;

    // 4. Make the next of prev_node as new_node
    prev_Node.next = new_node;

    // 5. Make prev_node as previous of new_node
    new_node.prev = prev_Node;

    // 6. Change previous of new_node's next node
    if (new_node.next != null)
        new_node.next.prev = new_node;
}


// This code is contributed by unknown2108

</script>
Python3
# Given a node as prev_node, insert
# a new node after the given node


def insertAfter(self, prev_node, new_data):

    # Check if the given prev_node is NULL
    if prev_node is None:
        print("This node doesn't exist in DLL")
        return

    # 1. allocate node  & 
    # 2. put in the data
    new_node = Node(data=new_data)

    # 3. Make next of new node as next of prev_node
    new_node.next = prev_node.next

    # 4. Make the next of prev_node as new_node
    prev_node.next = new_node

    # 5. Make prev_node as previous of new_node
    new_node.prev = prev_node

    # 6. Change previous of new_node's next node
    if new_node.next is not None:
        new_node.next.prev = new_node

#  This code is contributed by jatinreaper

Time Complexity: O(1)
Auxiliary Space: O(1)

2. Add a node before a given node in a Doubly Linked List: 

Let the pointer to this given node be next_node. This can be done using the following steps. 

  • Allocate memory for the new node, let it be called new_node.
  • Put the data in new_node.
  • Set the previous pointer of this new_node as the previous node of the next_node.
  • Set the previous pointer of the next_node as the new_node.
  • Set the next pointer of this new_node as the next_node.
  • Set the next pointer of the previous of new_node to new_node.

Below is the implementation of the above approach.

C++
// Given a node as prev_node, insert a new node 
// after the given node
void insertBefore(Node* next_node, int new_data)
{
    // Check if the given next_node is NULL
    if (next_node == NULL) {
        printf("the given next node cannot be NULL");
        return;
    }

    // 1. Allocate new node
    Node* new_node = new Node();

    // 2. Put in the data
    new_node->data = new_data;

    // 3. Make previous of new node as previous of next_node
    new_node->prev = next_node->prev;

    // 4. Make the previous of next_node as new_node
    next_node->prev = new_node;

    // 5. Make next_node as next of new_node
    new_node->next = next_node;

    // 6. Change next of new_node's previous node
    if (new_node->prev != NULL)
        new_node->prev->next = new_node;
    else
        head = new_node;
}
C
// Given a node as prev_node, insert a new node 
// after the given node
void insertBefore(struct Node* next_node, int new_data)
{
    // Check if the given next_node is NULL
    if (next_node == NULL) {
        printf("the given next node cannot be NULL");
        return;
    }

    // 1. Allocate new node
    struct Node* new_node
        = (struct Node*)malloc(sizeof(struct Node));

    // 2. Put in the data
    new_node->data = new_data;

    // 3. Make previous of new node as previous of next_node
    new_node->prev = next_node->prev;

    // 4. Make the previous of next_node as new_node
    next_node->prev = new_node;

    // 5. Make next_node as next of new_node
    new_node->next = next_node;

    // 6. Change next of new_node's previous node
    if (new_node->prev != NULL)
        new_node->prev->next = new_node;
    else
        head = new_node;
}
Java
// Given a node as prev_node, insert a new node 
// after the given node
public void InsertBefore(Node next_Node, int new_data)
{
    // Check if the given next_node is NULL
    if (next_Node == null) {
        System.out.println(
            "The given next node cannot be NULL ");
        return;
    }

    // 1. Allocate node
    // 2. Put in the data 
    Node new_node = new Node(new_data);

    // 3. Make previous of new node as previous of prev_node
    new_node.prev = next_Node.prev;

    // 4. Make the prev of next_node as new_node
    next_Node.prev = new_node;

    // 5. Make next_node as next of new_node
    new_node.next = next_Node;

    // 6. Change next of new_node's previous node
    if (new_node.prev != null)
        new_node.prev.next = new_node;
    else
        head = new_node;
}
C#
// Given a node as prev_node, insert a new node 
// after the given node
public void InsertAfter(Node next_Node, int new_data)
{
    // Check if the given next_node is NULL
    if (next_Node == null) {
        Console.WriteLine(
            "The given next node cannot be NULL ");
        return;
    }

    // 1. Allocate node
    // 2. Put in the data
    Node new_node = new Node(new_data);

    // 3. Make previous of new node as previous of next_node
    new_node.prev = next_Node.prev;

    // 4. Make the previous of next_node as new_node
    next_Node.prev = new_node;

    // 5. Make next_node as next of new_node
    new_node.next = next_Node;

    // 6. Change next of new_node's previous node
    if (new_node.prev != null)
        new_node.prev.next = new_node;
    else
        head = new_node;
}

// This code is contributed by aashish1995
Javascript
<script>

function InsertAfter(next_Node,new_data)
{
        // Check if the given next_Node is NULL
    if (next_Node == null) {
        document.write("The given next node cannot be NULL <br>");
        return;
    }

    // 1. Allocate node 
    // 2. Put in the data
    let new_node = new Node(new_data);

    // 3. Make previous of new node as previous of next_node
    new_node.prev = next_Node.prev;

    // 4. Make the previous of next_node as new_node
    next_Node.prev = new_node;

    // 5. Make next_node as next of new_node
    new_node.next = next_Node;

    // 6. Change next of new_node's previous node
    if (new_node.prev != null)
        new_node.prev.next = new_node;
    else
        head = new_node;
}


// This code is contributed by unknown2108

</script>
Python3
# Given a node as prev_node, insert
# a new node after the given node


def insertAfter(self, next_node, new_data):

    # Check if the given next_node is NULL
    if next_node is None:
        print("This node doesn't exist in DLL")
        return

    # 1. Allocate node  & 
    # 2. Put in the data
    new_node = Node(data=new_data)

    # 3. Make previous of new node as previous of prev_node
    new_node.prev = next_node.prev

    # 4. Make the previous of next_node as new_node
    next_node.prev = new_node

    # 5. Make next_node as next of new_node
    new_node.next = next_node

    # 6. Change next of new_node's previous node
    if new_node.prev is not None:
        new_node.prev.next = new_node
    else:
        head = new_node

#  This code is contributed by jatinreaper

Time Complexity: O(1)
Auxiliary Space: O(1)

Insertion at the End in Doubly Linked List:

Insertion_end_Doubly-Linked-List

The new node is always added after the last node of the given Linked List. This can be done using the following steps:

  • Create a new node (say new_node).
  • Put the value in the new node.
  • Make the next pointer of new_node as null.
  • If the list is empty, make new_node as the head.
  • Otherwise, travel to the end of the linked list.
  • Now make the next pointer of last node point to new_node.
  • Change the previous pointer of new_node to the last node of the list.

Below is the implementation of the steps to insert a node at the end of the linked list:

C++
// Given a reference (pointer to pointer) to the head
// of a DLL and an int, appends a new node at the end
void append(Node** head_ref, int new_data)
{
    // 1. allocate node
    Node* new_node = new Node();

    Node* last = *head_ref; /* used in step 5*/

    // 2. put in the data
    new_node->data = new_data;

    // 3. This new node is going to be the last node, so
    // make next of it as NULL
    new_node->next = NULL;

    // 4. If the Linked List is empty, then make the new
    // node as head
    if (*head_ref == NULL) {
        new_node->prev = NULL;
        *head_ref = new_node;
        return;
    }

    // 5. Else traverse till the last node
    while (last->next != NULL)
        last = last->next;

    // 6. Change the next of last node
    last->next = new_node;

    // 7. Make last node as previous of new node
    new_node->prev = last;

    return;
}

// This code is contributed by shivanisinghss2110
C
// Given a reference (pointer to pointer) to the head
// of a DLL and an int, appends a new node at the end
void append(struct Node** head_ref, int new_data)
{
    // 1. allocate node
    struct Node* new_node
        = (struct Node*)malloc(sizeof(struct Node));

    struct Node* last = *head_ref; /* used in step 5*/

    // 2. put in the data
    new_node->data = new_data;

    // 3. This new node is going to be the last node, so
    // make next of it as NULL
    new_node->next = NULL;

    // 4. If the Linked List is empty, then make the new
    // node as head
    if (*head_ref == NULL) {
        new_node->prev = NULL;
        *head_ref = new_node;
        return;
    }

    // 5. Else traverse till the last node
    while (last->next != NULL)
        last = last->next;

    // 6. Change the next of last node
    last->next = new_node;

    // 7. Make last node as previous of new node
    new_node->prev = last;

    return;
}
Java
// Add a node at the end of the list
void append(int new_data)
{
    // 1. allocate node
    // 2. put in the data
    Node new_node = new Node(new_data);

    Node last = head; /* used in step 5*/

    // 3. This new node is going to be the last node, so
    // make next of it as NULL
    new_node.next = null;

    // 4. If the Linked List is empty, then make the new
    // node as head
    if (head == null) {
        new_node.prev = null;
        head = new_node;
        return;
    }

    // 5. Else traverse till the last node
    while (last.next != null)
        last = last.next;

    // 6. Change the next of last node
    last.next = new_node;

    // 7. Make last node as previous of new node
    new_node.prev = last;
}
C#
// Add a node at the end of the list
void append(int new_data)
{
    // 1. allocate node
    // 2. put in the data
    Node new_node = new Node(new_data);

    Node last = head; /* used in step 5*/

    // 3. This new node is going
    //  to be the last node, so
    // make next of it as NULL
    new_node.next = null;

    // 4. If the Linked List is empty,
    // then make the new node as head
    if (head == null) {
        new_node.prev = null;
        head = new_node;
        return;
    }

    // 5. Else traverse till the last node
    while (last.next != null)
        last = last.next;

    // 6. Change the next of last node
    last.next = new_node;

    // 7. Make last node as previous of new node
    new_node.prev = last;
}

// This code is contributed by shivanisinghss2110
Javascript
<script>
// Add a node at the end of the list
function append(new_data)
{
    /* 1. allocate node 
     * 2. put in the data */
    var new_node = new Node(new_data);

    var last = head; /* used in step 5*/

    /* 3. This new node is going to be the last node, so
     * make next of it as NULL*/
    new_node.next = null;

    /* 4. If the Linked List is empty, then make the new
     * node as head */
    if (head == null) {
        new_node.prev = null;
        head = new_node;
        return;
    }

    /* 5. Else traverse till the last node */
    while (last.next != null)
        last = last.next;

    /* 6. Change the next of last node */
    last.next = new_node;

    /* 7. Make last node as previous of new node */
    new_node.prev = last;
}

// This code is contributed by Rajput-Ji 
</script>
Python3
# Add a node at the end of the DLL
def append(self, new_data):

    # 1. allocate node 
    # 2. put in the data
    new_node = Node(data=new_data)
    last = self.head

    # 3. This new node is going to be the
    # last node, so make next of it as NULL
    new_node.next = None

    # 4. If the Linked List is empty, then
    #  make the new node as head
    if self.head is None:
        new_node.prev = None
        self.head = new_node
        return

    # 5. Else traverse till the last node
    while (last.next is not None):
        last = last.next

    # 6. Change the next of last node
    last.next = new_node
    # 7. Make last node as previous of new node */
    new_node.prev = last

#  This code is contributed by jatinreaper

Time Complexity: O(n)
Auxiliary Space: O(1)

Related Articles:

 



Previous Article
Next Article

Similar Reads

Insertion at Specific Position in a Circular Doubly Linked List
Prerequisite: Insert Element Circular Doubly Linked List.Convert an Array to a Circular Doubly Linked List.Given the start pointer pointing to the start of a Circular Doubly Linked List, an element and a position. The task is to insert the element at the specified position in the given Circular Doubly Linked List. Recommended: Please try your appro
15+ min read
Insertion in Doubly Circular Linked List
Circular Doubly Linked List has properties of both doubly linked list and circular linked list in which two consecutive elements are linked or connected by the previous and next pointer and the last node points to the first node by the next pointer and also the first node points to the last node by the previous pointer. Following is the representat
15+ min read
Insertion Sort for Doubly Linked List
Sort the doubly linked list using the insertion sort technique. Initial doubly linked list Doubly Linked List after applying insertion sort Algorithm: Below is a simple insertion sort algorithm for doubly-linked lists.1) Create an empty sorted (or result) doubly linked list. 2) Traverse the given doubly linked list, and do the following for every n
14 min read
Difference between Singly linked list and Doubly linked list
Introduction to Singly linked list : A singly linked list is a set of nodes where each node has two fields 'data' and 'link'. The 'data' field stores actual piece of information and 'link' field is used to point to next node. Basically the 'link' field stores the address of the next node. Introduction to Doubly linked list : A Doubly Linked List (D
2 min read
When is Doubly Linked List more Efficient than Singly Linked List?
Did you know there are some cases where a Doubly Linked List is more efficient than a Singly Linked List, even though it takes more memory compared to a Singly Linked List? What are those Cases? Well, we will discuss that in the following article, But first, let's talk about Singly and linked lists: What is a Singly Linked List?A singly linked list
4 min read
Is two way linked list and doubly linked list same?
Yes, a two-way linked list and a doubly linked list are the same. Both terms refer to a type of linked list where each node contains a reference to the next node as well as the previous node in the sequence. The term “two-way” emphasizes the ability to move in both directions through the list, while “doubly” highlights that there are two links per
3 min read
XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
In the previous post, we discussed how a Doubly Linked can be created using only one space for the address field with every node. In this post, we will discuss the implementation of a memory-efficient doubly linked list. We will mainly discuss the following two simple functions. A function to insert a new node at the beginning.A function to travers
10 min read
XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
In this post, we're going to talk about how XOR linked lists are used to reduce the memory requirements of doubly-linked lists. We know that each node in a doubly-linked list has two pointer fields which contain the addresses of the previous and next node. On the other hand, each node of the XOR linked list requires only a single pointer field, whi
15+ min read
Construct a Doubly linked linked list from 2D Matrix
Given a 2D matrix, the task is to convert it into a doubly-linked list with four pointers that are next, previous, up, and down, each node of this list should be connected to its next, previous, up, and down nodes.Examples: Input: 2D matrix 1 2 3 4 5 6 7 8 9 Output: Approach: The main idea is to construct a new node for every element of the matrix
15+ min read
Insertion in Unrolled Linked List
An unrolled linked list is a linked list of small arrays, all of the same size where each is so small that the insertion or deletion is fast and quick, but large enough to fill the cache line. An iterator pointing into the list consists of both a pointer to a node and an index into that node containing an array. It is also a data structure and is a
11 min read
three90RightbarBannerImg