 Hello, everyone. I am Mrs. Meenakshi Shree Gandhi from Walshian Institute of Technology, Sholapur. Welcome to the video lecture on Insertion of a Node in a Link List. Learning outcome. At the end of this session, students will be able to explain the operation of insertion of a node in a singularly linked list. Definition of linked list. A linked list is a linear data structure in which the elements are not stored at contiguous memory locations. It is a data structure consisting of a collection of nodes which together represents a sequence. The elements in a linked list are linked using pointers as shown in the below image. So this figure represents a linked list which is nothing but the collection of nodes. So these elements are representing the node. The first node is pointed to by the pointer head and the last node is terminated by a null. Let's see the definition of node in a linked list. Node is an element in a linked list. Node consists of two parts. The first part is the data part which contains information and the second part is called as the address part which is nothing but the pointer to the next node and hence it is also known as the next part. This figure represents a node which shows that a node contains two parts. The first part is called as the data part which contains some information and the second part is called as the address part which is having the address of next node. So that's why the address is also called as the next part. Let's see the operation which can be performed on a linked list. There are different operations that can be performed on a linked list. Those are determine if the list is empty or not, insertion of an element in the list, deletion of an element from the list to search for a particular element in the list and display the elements in the list. These are the different operations which can be performed on a linked list but in this video we are only going to focus for insertion of the element in the linked list. Let's see the insertion of a node in a single linked list. Insertion of a node means adding a given element at the required location in the linked list. This process involves two steps that is first to find the correct location for insertion and the second is insertion of the given node at that location. There are three ways for insertion of a node in a linked list. These are at the front of the linked list, after a given node and at the end of the linked list. Let's see the first type that is insertion of a node in front of the linked list. For insertion of a node in front of the linked list there is no work to be done to find the location. Here we are directly inserting the new node at the first place of the linked list. But for that first we need to check for the linked list is empty or not. If the linked list is empty then the new node will be the first node of the linked list. This can be shown by an example. This is a new node which has to be inserted in the given linked list. But first we are checking if the linked list is empty or not. So if the linked list is currently empty then this new node will be the first node of the linked list. For that the pointer ll will be assigned to this new node. So this pointer has been assigned to this new node and now the linked list will begin from this node onwards. But if the linked list is not empty then the next part of the new node will point to the head node. This can be illustrated by an example. Here is a linked list consisting of three nodes. The first node is pointed to by the pointer ll and the last node has been terminated by a null. And our task is to insert this new node as the first node. For this we only have to assign the next field of the new node to the pointer ll. After that is done the linked list is updated in this way. We can see that the new node has been added as the first node to the linked list but only what we can see is that the pointer ll is now still pointing to the second node. So we need to update the pointer ll. For this the ll pointer is now assigned to the new node. So this is the updated linked list with the new node as the first node. Try to think and answer what is the algorithm for insertion of a node at friend for a single linked list. Pause the video for some time and note down the answer in your book. Algorithm for insertion of a node in friend for a single linked list. First we need to create a new node. Fill in the data part of the new node. Check if the linked list is empty or not. If empty then set the linked list pointer to point to the new pointer but if not empty then set the next field of the new node to point to ll pointer. And after that is done update the current ll pointer to point to the new node. Let's see the second way that is insertion of a node at the end of the linked list. Here we need to find the end of the linked list. For finding this we need to traverse the linked list through iteration till null is found. After that is done we need to add the given node at the end of the linked list. This can be illustrated by an example. There is a linked list which consists of three nodes. The first node is pointed to by the pointer ll and the last node has been terminated by a null. Our task is to add this new node as the last node to the linked list. For this first we need to assign a pointer which will traverse the linked list till it reaches the last node. This is the current pointer which has been traversed through all the nodes and it has reached to the last node of the linked list. After that is done then we need to assign the next part of the current to the new. After that is done this is the updated linked list. Here we can see that the new node has been added as the last node to the linked list. Try to think and answer what is the algorithm for insertion of a node at the end of the linked list. Pause the video for some time and note down the answer in your book. Algorithm for insertion of a node at the end of the singly linked list. First we need to create a new node. Then fill the data part of the new node. Then fill the next part of the new node as null. If we only traverse the current linked list till we reach the last node by using the current pointer. Then point the next part of the last node to the new node via the current pointer. Let's see the third way of insertion that is insertion of a node after a given node in the linked list. This type of insertion is most commonly used when order is important. For that we need to go to the node that should follow the one to add through recursion or iteration. This can be best explained by using illustration. So consider an example. Here is a linked list consisting of 3 nodes and the first node has been pointed to by the pointer ln and this is the last node which has been terminated by a null. And our task is to add this new node after 50 and before 80. For this we need to first assign a pointer current which will traverse through all the nodes till it reaches to the node after which we need to add this new node. Suppose after traversing the current pointer reaches the node after which we need to add the new node. So after that has been done then we need to assign the next of new to the next of current node. And after that the next of the current node should now be assigned to new. After that has been done you can see that the new node with value 70 has been added after 50 and before 80 and this is the updated linked list. Try to think an answer. What is the algorithm for insertion of a node after a given node in the linked list? Pause the video for some time and note down the answer in your book. The algorithm for insertion of a node after a given node in the single linked list. For this first we need to create a new node, then fill the data part of the new node, fill the next part of the new node as null, then recursively travels the current linked list via the current pointer till the node that should follow the one to add. After that point the next part of the new node to the next node of the current node. After that point the next part of the current node to the new node. These are my references. Thank you.