Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Insertion in Linked list | Data structures

YASH PAL, 12 May 202028 May 2024

Insertion – 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

After completing this tutorial you are able to 

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

Insert the node beginning of the 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 the image given below.

Insertion - Linked list Data structures and algorithms

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.

Insertion - Linked list Data structures and algorithms
Insertion - Linked list Data structures and algorithms

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 the infinite loop of the list.

Insertion - Linked list Data structures and algorithms
def insert_in_beginning(self, data):
    temp = Node(data)
    temp.link = self.start
    self.start = temp

Insert the node in an empty 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.

Insertion - Linked list Data structures and algorithms

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

Insertion - Linked list Data structures and algorithms
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 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.

Insertion - Linked list Data structures and algorithms

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

Insertion - Linked list Data structures and algorithms
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 in between the nodes. 

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 the image. and we allocate a new node called temp.

Insertion - Linked list Data structures and algorithms

and then we store the reference of the next node of p into the temp node as you see in the image given below.

Insertion - Linked list Data structures and algorithms

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

Insertion - Linked list Data structures and algorithms

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.

Insertion - Linked list Data structures and algorithms

Insert a node after a node

like if we want to insert the node after a node that has value x as you see in the image given below.

Insertion - Linked list Data structures and algorithms

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

Insertion - Linked list Data structures and algorithms

and then we allocate a new node called temp.

Insertion - Linked list Data structures and algorithms

and then we store the reference of the next node to the new node as you see in the image.

Insertion - Linked list Data structures and algorithms

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

Insertion - Linked list Data structures and algorithms
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

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. then first we need to find the reference to the predecessor of the node that has value x. 

Insertion - Linked list Data structures and algorithms

and then we allocate a new node called temp.

Insertion - Linked list Data structures and algorithms

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

Insertion - Linked list Data structures and algorithms

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

Insertion - Linked list Data structures and algorithms
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.

like 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 the image given below.

Insertion - Linked list Data structures and algorithms

after that, we allocate a new node called temp.

Insertion - Linked list Data structures and algorithms

then we store the reference of the node at the kth position. as you see in the image given below.

Insertion - Linked list Data structures and algorithms

then we store the reference of the newly allocated node to the k-1 node. as you see in the image given below.

Insertion - Linked list Data structures and algorithms

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

Insertion - Linked list Data structures and algorithms
Computer Science Tutorials Data Structures Tutorials computer scienceData Structure

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes