Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone

Insertion in Doubly Linked List Data Structure | DSA Tutorials

YASH PAL, 20 May 20204 May 2026

Insertion in the doubly linked list – As we know, in the node of the doubly linked list, we have two link parts and one info part. and every node holds a reference to the previous and next node. So to insert the new node at any position, like at the first position, the last position, in between the nodes, and before and after the node, we need to maintain both references.

So after completing this tutorial, you will be able to learn about

  1. Insertion at the beginning of the doubly linked list
  2. Insert node in an empty list.
  3. Insert node at the end of the list.
  4. Insert a node after a node.
  5. Insert a node before a node.

Insertion in Doubly Linked List

Insert at the beginning of the doubly linked list

To insert a node at the beginning of the list first, we allocate a new node called temp, as you see in Figure 1.

Insert at the beginning of the doubly linked list
Figure 1: Insert at the beginning of the doubly linked list

After that, we store the first node’s reference in the temp node’s next link part.

Insert at the beginning of the doubly linked list
Figure 2: Insertion in a doubly Linked List

And then we store the new node temp’s reference in the first node of the list, as you see in Figure 3.

Insert at the beginning of the doubly linked list
Figure 3: Insertion in Doubly Linked List

After performing these steps, we inserted a new node at the beginning of the linked list.

Insert a node in an Empty Doubly Linked List

As we know, we don’t have any node in an empty list.

Insert a node in an empty doubly linked list
Figure 4: Insert a node in an empty doubly linked list

So, to insert a node in an empty list, first we allocate a new node called temp and store the reference of the new node in the start variable.

Insert a node in an empty doubly linked list
Figure 5: Insert a node in an empty doubly linked list

Insert a node at the end of the Doubly Linked List

To insert a new node at the end of the list, first we need a reference to the last node of the doubly linked list. So we find a reference for the last node called p.

Insert a node at the end of the Doubly Linked List
Figure 6: Insert a node at the end of the Doubly Linked List

After that, we allocate a new node called temp.

insertion in doubly linked list

And then we store the new node’s reference in the next link part of the node p.

insertion in doubly linked list

And then we store the p node’s reference into the new node’s previous link part.

insertion in doubly linked list

After performing these steps, we have now inserted a new node at the end of the list.

Insert a node after a node

If we want to insert a node after a node called p.

Insert a node after a node
Figure 7: Insert a node after a node

Then we first allocate a new node called temp.

insertion in doubly linked list

Then store the p node’s reference into the temp node’s previous link part.

insertion in doubly linked list

Then we store the node’s reference that comes after the node p into the temp node’s next link part.

insertion in doubly linked list

Then we store the temp node’s reference into the node’s previous link part that comes after the node p.

insertion in doubly linked list

Then we store the temp node’s reference into the p node’s next link part.

insertion in doubly linked list

After completing these steps, we have now inserted a new node before a node called p in the doubly linked list.

Insert a node before a node

If we want to insert a node before a node called p.

Insert a node before a node
Figure 7: Insert a node before a node

First, we allocate a new node called temp.

insertion in doubly linked list

Then we store the node’s reference that comes before the node p into the temp node’s previous link.

insertion in doubly linked list

Then we store the reference of the p node into the temp node’s next link part.

insertion in doubly linked list

After that, we store the temp node’s reference into the next link part of the node that comes before the node p.

insertion in doubly linked list

After that, we store the temp node’s reference into the previous link part of the node p.

insertion in doubly linked list

After performing these steps, we have now inserted a new node before a node called p.

Python program for insertion in a Doubly Linked List

class Node(object):

    def __init__(self, value):
        self.info = value
        self.link = None

class CircularLinkedList(object):

    def __init__(self):
        self.last = None
    def display_list(self):

        if self.last == None:
            print("List is empty\n")
            return
        p = self.last.link

        while True:
            print(p.info, " ", end='')
            p = p.link
            if p == self.last.link:
                break        print()

    def insert_in_beginning(self, data):
        temp = Node(data)
        temp.link = self.last.link
        self.last.link = temp

    def insert_in_empty_list(self, data):
        temp = Node(data)
        self.last = temp
        self.last.link = self.last

    def insert_at_end(self,data):
        temp = Node(data)
        temp.link = self.last.link
        self.last.link = temp
        self.last = temp

    def create_list(self):
        n = int(input("Enter the number of nodes : "))
        if n == 0:
            return        data = int(input("Enter the element to be inserted : "))
        self.insert_in_empty_list(data)

        for i in range(n-1):
            data = int(input("Enter the element to be inserted : "))
            self.insert_at_end(data)

    def insert_after(self, data, x):
        p = self.last.link

        while True:
            if p.info == x:
                break            p = p.link
            if p == self.last.link:
                break
        if p == self.last.link and p.info != x:
            print(x, " not present in the list")
        else:
            temp = Node(data)
            temp.link = p.link
            p.link = temp
            if p == self.last:
                self.last = temp

    def delete_first_node(self):
        if self.last is None:
            return        if self.last.link == self.last:
            self.last = None            return        self.last.link = self.last.link.link

    def delete_last_node(self):
        if self.last is None:
            return        if self.last.link == self.last:
            self.last = None            return
        p = self.last.link
        while p.link != self.last:
            p = p.link
        p.link = self.last.link
        self.last = p

    def delete_node(self,x):

        if self.last is None:
            return        if self.last.link == self.last and self.last.info == x:
            self.last = None            return        if self.last.link.info == x:
            self.last.link == self.last.link.link
            return
        p = self.last.link
        while p.link != self.last.link:
            if p.link.info == x:
                break            p = p.link

        if p.link == self.last.link:
            print(x, " not found in the list")
        else:
            p.link = p.link.link
            if self.last.info == x:
                self.last = p

#################################################

list = CircularLinkedList()


list.create_list()

while True:
    print("1. Display list")
    print("2. Insert in empty list")
    print("3. Insert a node in the beginning of the list")
    print("4. Insert a node at the end of the list")
    print("5. Insert a node after a specified node")
    print("6. Delete first node")
    print("7. Delete last node")
    print("8. Delete any node")
    print("9. Quite")

    option = int(input("Enter your choice : "))

    if option == 1:
        list.display_list()
    elif option == 2:
        data = int(input("Enter the element to be inserted : "))
        list.insert_in_empty_list(data)
    elif option == 3:
        data = int(input("Enter the element to be inserted : "))
        list.insert_in_beginning(data)
    elif option == 4:
        data = int(input("Enter the element to be inserted : "))
        list.insert_at_end(data)
    elif option == 5:
        data = int(input("Enter the element to be inserted : "))
        x = int(input("Enter the element after which to insert : "))
        list.insert_after(data,x)
    elif option == 6:
        list.delete_first_node()
    elif option == 7:
        list.delete_last_node()
    elif option == 8:
        data = int(input("Enter the element to be deleted : "))
        list.delete_node(data)
    elif option == 9:
        break    else:
        print("Wrong option")
    print()

Data Structures & Algorithms Tutorials for Beginners
Computer Science Tutorials Data Structures Tutorials computer scienceData StructureDSA Tutorials

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes