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.
Add a node at the front in a Doubly Linked List:
The new node is always added before the head of the given Linked List. The task can be performed by using the following 5 steps:
- Firstly, allocate a new node (say new_node).
- Now put the required data in the new node.
- Make the next of new_node point to the current head of the doubly linked list.
- Make the previous of the current head point to new_node.
- Lastly, point head to new_node.
Illustration:
See the below illustration where E is being inserted at the beginning of the doubly linked list.

Below is the implementation of the 5 steps to insert a node at the front of the linked list:
C
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
|
C++
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
|
Java
public void push( int new_data)
{
Node new_Node = new Node(new_data);
new_Node.next = head;
new_Node.prev = null ;
if (head != null )
head.prev = new_Node;
head = new_Node;
}
|
Python3
def push( self , new_data):
new_node = Node(data = new_data)
new_node. next = self .head
new_node.prev = None
if self .head is not None :
self .head.prev = new_node
self .head = new_node
|
C#
public void push( int new_data)
{
Node new_Node = new Node(new_data);
new_Node.next = head;
new_Node.prev = null ;
if (head != null )
head.prev = new_Node;
head = new_Node;
}
|
Javascript
function push(new_data)
{
let new_Node = new Node(new_data);
new_Node.next = head;
new_Node.prev = null ;
if (head != null )
head.prev = new_Node;
head = new_Node;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Add a node in between two nodes:
It is further classified into the following two parts:
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 6 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.
- Change the pointer of the new node’s previous pointer to new_node.
Illustration:
See the below illustration where ‘E‘ is being inserted after ‘B‘.

Below is the implementation of the 7 steps to insert a node after a given node in the linked list:
C
void insertAfter( struct Node* prev_node, int new_data)
{
if (prev_node == NULL) {
printf ( "the given previous node cannot be NULL" );
return ;
}
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
new_node->prev = prev_node;
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
|
C++
void insertAfter(Node* prev_node, int new_data)
{
if (prev_node == NULL) {
cout << "the given previous node cannot be NULL" ;
return ;
}
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
new_node->prev = prev_node;
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
|
Java
public void InsertAfter(Node prev_Node, int new_data)
{
if (prev_Node == null ) {
System.out.println(
"The given previous node cannot be NULL " );
return ;
}
Node new_node = new Node(new_data);
new_node.next = prev_Node.next;
prev_Node.next = new_node;
new_node.prev = prev_Node;
if (new_node.next != null )
new_node.next.prev = new_node;
}
|
Python3
def insertAfter( self , prev_node, new_data):
if prev_node is None :
print ( "This node doesn't exist in DLL" )
return
new_node = Node(data = new_data)
new_node. next = prev_node. next
prev_node. next = new_node
new_node.prev = prev_node
if new_node. next is not None :
new_node. next .prev = new_node
|
C#
public void InsertAfter(Node prev_Node, int new_data)
{
if (prev_Node == null ) {
Console.WriteLine(
"The given previous node cannot be NULL " );
return ;
}
Node new_node = new Node(new_data);
new_node.next = prev_Node.next;
prev_Node.next = new_node;
new_node.prev = prev_Node;
if (new_node.next != null )
new_node.next.prev = new_node;
}
|
Javascript
<script>
function InsertAfter(prev_Node,new_data)
{
if (prev_Node == null ) {
document.write( "The given previous node cannot be NULL <br>" );
return ;
}
let new_node = new Node(new_data);
new_node.next = prev_Node.next;
prev_Node.next = new_node;
new_node.prev = prev_Node;
if (new_node.next != null )
new_node.next.prev = new_node;
}
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
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 6 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.
- Now set the previous pointer of new_node.
- If the previous node of the new_node is not NULL, then set the next pointer of this previous node as new_node.
- Else, if the prev of new_node is NULL, it will be the new head node.
Illustration:
See the below illustration where ‘B‘ is being inserted before ‘C‘.

Below is the implementation of the above approach.
C
void insertBefore( struct Node* next_node, int new_data)
{
if (next_node == NULL) {
printf ( "the given next node cannot be NULL" );
return ;
}
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->prev = next_node->prev;
next_node->prev = new_node;
new_node->next = next_node;
if (new_node->prev != NULL)
new_node->prev->next = new_node;
else
head = new_node;
}
|
C++
void insertBefore(Node* next_node, int new_data)
{
if (next_node == NULL) {
printf ( "the given next node cannot be NULL" );
return ;
}
Node* new_node = new Node();
new_node->data = new_data;
new_node->prev = next_node->prev;
next_node->prev = new_node;
new_node->next = next_node;
if (new_node->prev != NULL)
new_node->prev->next = new_node;
else
head = new_node;
}
|
Java
public void InsertBefore(Node next_Node, int new_data)
{
if (next_Node == null ) {
System.out.println(
"The given next node cannot be NULL " );
return ;
}
Node new_node = new Node(new_data);
new_node.prev = next_Node.prev;
next_Node.prev = new_node;
new_node.next = next_Node;
if (new_node.prev != null )
new_node.prev.next = new_node;
else
head = new_node;
}
|
Python3
def insertAfter( self , next_node, new_data):
if next_node is None :
print ( "This node doesn't exist in DLL" )
return
new_node = Node(data = new_data)
new_node.prev = next_node.prev
next_node.prev = new_node
new_node. next = next_node
if new_node.prev is not None :
new_node.prev. next = new_node
else :
head = new_node
|
C#
public void InsertAfter(Node next_Node, int new_data)
{
if (next_Node == null ) {
Console.WriteLine(
"The given next node cannot be NULL " );
return ;
}
Node new_node = new Node(new_data);
new_node.prev = next_Node.prev;
next_Node.prev = new_node;
new_node.next = next_Node;
if (new_node.prev != null )
new_node.prev.next = new_node;
else
head = new_node;
}
|
Javascript
<script>
function InsertAfter(next_Node,new_data)
{
if (next_Node == null ) {
document.write( "The given next node cannot be NULL <br>" );
return ;
}
let new_node = new Node(new_data);
new_node.prev = next_Node.prev;
next_Node.prev = new_node;
new_node.next = next_Node;
if (new_node.prev != null )
new_node.prev.next = new_node;
else
head = new_node;
}
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Add a node at the end in a 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 7 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.
Illustration:
See the below illustration where ‘D‘ is inserted at the end of the linked list.

Below is the implementation of the 7 steps to insert a node at the end of the linked list:
C
void append( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
struct Node* last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
new_node->prev = NULL;
*head_ref = new_node;
return ;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
new_node->prev = last;
return ;
}
|
C++
void append(Node** head_ref, int new_data)
{
Node* new_node = new Node();
Node* last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
new_node->prev = NULL;
*head_ref = new_node;
return ;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
new_node->prev = last;
return ;
}
|
Java
void append( int new_data)
{
Node new_node = new Node(new_data);
Node last = head;
new_node.next = null ;
if (head == null ) {
new_node.prev = null ;
head = new_node;
return ;
}
while (last.next != null )
last = last.next;
last.next = new_node;
new_node.prev = last;
}
|
Python3
def append( self , new_data):
new_node = Node(data = new_data)
last = self .head
new_node. next = None
if self .head is None :
new_node.prev = None
self .head = new_node
return
while (last. next is not None ):
last = last. next
last. next = new_node
new_node.prev = last
|
C#
void append( int new_data)
{
Node new_node = new Node(new_data);
Node last = head;
new_node.next = null ;
if (head == null ) {
new_node.prev = null ;
head = new_node;
return ;
}
while (last.next != null )
last = last.next;
last.next = new_node;
new_node.prev = last;
}
|
Javascript
<script>
function append(new_data)
{
var new_node = new Node(new_data);
var last = head;
new_node.next = null ;
if (head == null ) {
new_node.prev = null ;
head = new_node;
return ;
}
while (last.next != null )
last = last.next;
last.next = new_node;
new_node.prev = last;
}
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Related Articles:
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.