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 listInsertion in Linked List Data StructureAfter completing this tutorial, you can Insert the node at the beginning of the linked listInsert the node in an empty linked listInsert the node at the end of the linked listInsert the node between the nodes of the linked listInsert the node after a node of the linked listInsert the node before a node of the linked listInsert the node at a given position in the linked listInsert the node at the beginning of the Linked ListTo 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.Figure 1: Insert the node at the beginning of the linked listAfter 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.Figure 2: Insert the node at the beginning of the linked listFigure 3: Insert the node at the beginning of the linked listNote: 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.Figure 4: Insert the node at the beginning of the linked listdef insert_in_beginning(self, data): temp = Node(data) temp.link = self.start self.start = tempdef insert_in_beginning(self, data): temp = Node(data) temp.link = self.start self.start = tempInsert the node in an empty Linked ListIn 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.Figure 5: Insert the node in an empty linked listFirst, we allocate a new node called temp, and then we point the reference of the node to the start variable.Figure 6: Insert the node in an empty linked listdef insert_at_end(self, data): temp = Node(data) if self.start is None: self.start = tempdef insert_at_end(self, data): temp = Node(data) if self.start is None: self.start = tempInsert the node at the end of the Linked ListTo 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.Figure 7: Insert the node at the end of the linked listAnd then we point the last node of the list to the newly allocated node.Figure 8: Insert the node at the end of the linked listdef 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 = tempdef 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 = tempInsert the node between the nodes of the Linked ListTo 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.Figure 9: Insert the node between the nodes of the linked listAnd then we store the reference of the next node of p into the temp node, as you see in Figure 10.Figure 10: Insert the node between the nodes of the linked listAnd then we store the reference of the temp node in the p node.Figure 11: Insert the node between the nodes of the linked listNote: 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.Figure 12: Insert the node between the nodes of the linked listInsert a node after a node of the linked listAs if we want to insert the node after a node that has value x, as you see in Figure 13.Figure 13: Insert a node after a node of the linked listFor 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.Figure 14: Insert a node after a node of the linked listAnd then we allocate a new node called temp.Figure 15: Insert a node after a node of the linked listAnd then we store the reference of the next node to the new node, as you see in Figure 16.Figure 16: Insert a node after a node of the linked listThen we store the reference of the newly allocated node in the p node.Figure 17: Insert a node after a node of the linked listdef 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 = tempdef 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 = tempInsert a node before a node of the linked listAs 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.Figure 18: Insert a node before a node of the linked listAnd then we allocate a new node called temp.Figure 19: Insert a node before a node of the linked listAnd then we store the reference of the node in the new node. Figure 20: Insert a node before a node of the linked listAnd then we store the reference of the new node in the p node.Figure 21: Insert a node before a node of the linked listdef 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 = tempdef 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 = tempInsert a node at a given position of the linked listIf 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.Figure 22: Insert a node at a given position of the linked listAfter that, we allocate a new node called temp.Figure 23: Insert a node at a given position of the linked listThen we store the reference of the node at the kth position, as you see in Figure 24. Figure 24: Insert a node at a given position of the linked listThen we store the reference of the newly allocated node to the k-1 node, as you see in Figure 25. Figure 25: Insert a node at a given position of the linked listSo the new node becomes the 4th node of the list.Data Structures & Algorithms Tutorials for Beginners Computer Science Tutorials Data Structures Tutorials computer scienceData StructureDSA Tutorials