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 Insert the node beginning of the list Insert the node in an empty list Insert the node at the end of the list Insert the node in between the nodes Insert the node after a node Insert the node before a node 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. 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. 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. 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. first, we allocate a new node called temp. and then we point the reference of the node to the start variable. 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. and then we point the last node of the list to the newly allocated node. 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. and then we store the reference of the next node of p into the temp node as you see in the image given below. and then we store the reference of the temp node in the p node. 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 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. 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. and then we allocate a new node called temp. and then we store the reference of the next node to the new node as you see in the image. then we store the reference of the newly allocated node in the p node. 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. and then we allocate a new node called temp. and then we store the reference of the node in the new node. and then we store the reference of the new node in the p node. 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. after that, we allocate a new node called temp. then we store the reference of the node at the kth position. as you see in the image given below. then we store the reference of the newly allocated node to the k-1 node. as you see in the image given below. so the new node becomes the 4th node of the list. Computer Science Tutorials Data Structures Tutorials computer scienceData Structure