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
Programmingoneonone

Insertion in Linked List Data Structure | DSA Tutorials

YASH PAL, 12 May 20204 May 2026

Insertion in Linked List – In a linked list, to insert a node at any position, like at the end, first, or in between the nodes, we need a reference to the node of the list. So first we need to find the reference to the node of the list. And if you don’t know how to find the reference to the node of the list, then first read this post – Operations on the Linked list

Insertion in Linked List Data Structure

After completing this tutorial, you can 

  1. Insert the node at the beginning of the linked list
  2. Insert the node in an empty linked list
  3. Insert the node at the end of the linked list
  4. Insert the node between the nodes of the linked list
  5. Insert the node after a node of the linked list
  6. Insert the node before a node of the linked list
  7. Insert the node at a given position in the linked list

Insert the node at the beginning of the Linked List

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

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

After that, we point the linked part of the new node to the first node of the list. And then we point the start variable to the temp node. So it will become the first node of the list.

Insert the node at the beginning of the linked list
Figure 2: Insert the node at the beginning of the linked list
Insert the node at the beginning of the linked list
Figure 3: Insert the node at the beginning of the linked list

Note: the process and steps we follow are very important because if we reverse the order of steps to insert the node, then we are stuck in an infinite loop of the list.

Insert the node at the beginning of the linked list
Figure 4: Insert the node at the beginning of the linked list
def insert_in_beginning(self, data):
    temp = Node(data)
    temp.link = self.start
    self.start = temp

Insert the node in an empty Linked List

In the empty linked list, we don’t have any nodes. So till now, our list is empty, the reference to the first node is null or None.

Insert the node in an empty linked list
Figure 5: Insert the node in an empty linked list

First, we allocate a new node called temp, and then we point the reference of the node to the start variable.

Insert the node in an empty linked list
Figure 6: Insert the node in an empty linked list
def insert_at_end(self, data):
    temp = Node(data)
    if self.start is None:
        self.start = temp

Insert the node at the end of the Linked List

To insert a node at the end of the list, first we need a reference to the last node of the list. after finding the reference to the last node of the list. First, we allocate a new node called temp.

Insert the node at the end of the linked list
Figure 7: Insert the node at the end of the linked list

And then we point the last node of the list to the newly allocated node.

Insert the node at the end of the linked list
Figure 8: Insert the node at the end of the linked list
def insert_at_end(self, data):
    temp = Node(data)
    if self.start is None:
        self.start = temp
        return
    p = self.start
    while p.link is not None:
        p = p.link
    p.link = temp

Insert the node between the nodes of the Linked List

To insert a node between the nodes, we need a reference to the node after we are going to insert the node, as you see in Figure 9. And we allocate a new node called temp.

Insert the node between the nodes of linked list
Figure 9: Insert the node between the nodes of the linked list

And then we store the reference of the next node of p into the temp node, as you see in Figure 10.

Insert the node between the nodes of linked list
Figure 10: Insert the node between the nodes of the linked list

And then we store the reference of the temp node in the p node.

Insert the node between the nodes of linked list
Figure 11: Insert the node between the nodes of the linked list

Note: the order of the steps to insert the new node between the nodes of the list is very important. If we reverse our steps, then we are stuck in an infinite loop.

Insert the node between the nodes of linked list
Figure 12: Insert the node between the nodes of the linked list

Insert a node after a node of the linked list

As if we want to insert the node after a node that has value x, as you see in Figure 13.

Insert a node after a node of the linked list
Figure 13: Insert a node after a node of the linked list

For example, we need to insert the node after the node that has the value x=56, then first we need to find the reference to the node that has the particular value.

Insert a node after a node of the linked list
Figure 14: Insert a node after a node of the linked list

And then we allocate a new node called temp.

Insert a node after a node of the linked list
Figure 15: Insert a node after a node of the linked list

And then we store the reference of the next node to the new node, as you see in Figure 16.

Insert a node after a node of the linked list
Figure 16: Insert a node after a node of the linked list

Then we store the reference of the newly allocated node in the p node.

Insert a node after a node of the linked list
Figure 17: Insert a node after a node of the linked list
def insert_after(self, data, x):
    p = self.start
    while p is not None:
        if p.info == x:
            break        p = p.link

    if p is None:
        print(x, "not present in the list")
    else:
        temp = Node(data)
        temp.link = p.link
        p.link = temp

Insert a node before a node of the linked list

As if we want to insert the node before a node that has the value x. For example, we need to insert the node before the node that has the value x=45. First, we need to find the reference to the predecessor of the node that has value x.

Insert a node before a node of the linked list
Figure 18: Insert a node before a node of the linked list

And then we allocate a new node called temp.

Insertion in linked list
Figure 19: Insert a node before a node of the linked list

And then we store the reference of the node in the new node.

Insertion in linked list
Figure 20: Insert a node before a node of the linked list

And then we store the reference of the new node in the p node.

Insertion in linked list
Figure 21: Insert a node before a node of the linked list
def insert_before(self, data, x):
    # If list is empty    if self.start is None:
        print("List is empty")
        return    # x is in first node , ew node is to be inserted before first node    if x == self.start.info:
        temp = Node(data)
        temp.link = self.start
        self.start = temp
        return
    # Find reference to predecessor of node contanining x    p = self.start
    while p.link is not None:
        if p.link.info == x:
            break        p = p.link

    if p.link is None:
        print(x, " not present in the list")
    else:
        temp = Node(data)
        temp.link = p.link
        p.link = temp

Insert a node at a given position of the linked list

If we want to insert a node at the kth position in the list. Then, first, we need to find the reference to the node at the k-1 position. For example, if we want to insert the node at the position k=4, then we find the reference to the node at the 3rd position. As you see in Figure 22.

Insert a node at a given position of the linked list
Figure 22: Insert a node at a given position of the linked list

After that, we allocate a new node called temp.

Insertion in linked list
Figure 23: Insert a node at a given position of the linked list

Then we store the reference of the node at the kth position, as you see in Figure 24.

Insertion in linked list
Figure 24: Insert a node at a given position of the linked list

Then we store the reference of the newly allocated node to the k-1 node, as you see in Figure 25.

Insertion in linked list
Figure 25: Insert a node at a given position of the linked list

So the new node becomes the 4th node of the list.

Insertion in linked list

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