 Hello everyone, I am Mrs. Meenakshi Shrigandhi from Balchen Institute of Technology, Shalapur. Welcome to the video lecture on deletion of a node from a linked list. Learning outcome. At the end of this session, students will be able to explain the operation of deletion of a node from a single 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. This image represents a linked list which is nothing but the collection of nodes. So here these individual elements are representing the nodes and here you can see that each node is having a different location and these are linked by using some pointers and the header node is pointed to by the head node and the last node is terminated by annul. Definition of node in a linked list. A node is an element in a linked list. It consists of two parts that is a data part which contains the information and the address part which is nothing but a pointer to next node hence also known as the next part. This represents a node which consists of two parts. The first part is the data part which contains the information and the second part is the address part which stores the address of the next node hence also called as next. Types of linked list. There are three types of linked list. The first is the single linked list. So here you can see that this linked list is having only a single link to the next node. Next is the circular linked list. A circular linked list is not terminated by annul. Instead a link is taken from the last node and it is pointed to the first node. The next is the double linked list. In a double linked list there are two links. One link is going to the next node and the other link is going to the previous node. Operations on linked list. There are different operations that can be performed on a linked list. Those are determine if list is until or not, insertion of an element in the list, deletion of an element from the list, search for a particular element in the list and display the elements in the list. In this video we are going to focus on deletion of an element from the list. Let's see the deletion of a node from a single linked list. Deletion of a node means removing the element of interest from the linked list. This process involves two steps, that is first finding the element to be deleted, then deletion of the node with pointer adjustment. There are two ways for deletion of a node from the linked list. Those are first node of the linked list and from anywhere in the linked list. Let's see the first one, that is deletion of first node from the linked list. In this time there is no work to find the location, only we need to check for the list is empty or not. If the list is empty, no node to delete from the linked list. So here it can be seen that the linked list is initially empty, if it is empty only we need to print that no node to delete from the linked list. But if the linked list is not empty, then simply make the external pointer list equal to the next field of the first node in the list. This is illustrated in this example, here is a linked list which consists of three nodes and the header node is pointed to by the list and the last node is terminated by a null. Our task is to delete the first node from this linked list. For that we are assigning our dummy pointer P to the first node. We are actually deletion of a node means we are going for logical deletion. So in this the list pointer is removed from the first node and it is moved to the next node. So after deletion you can see that the deleted node is now pointed by the pointer P and this is the updated linked list with only two nodes. Trying to think an answer, what is the algorithm for deletion of first node from a single linked list? Pause the video for some time and note down the answer in your book. Algorithm for deletion of first node from the single linked list. Input from the user the node to be deleted, say X, check if the list is empty or not. If empty then no node to be deleted. If not empty then set the P pointer to the first node that is node to be deleted and make the external pointer list equal to the next field of the first node. Let's see the deletion of a node which may be placed anywhere in the single linked list. There are three sub operations that is to find the node containing value X, modify the pointers to delete the node and free the node. In this let's see the first operation that is to find the node containing the value X. For that we need two pointers to keep track of the current node and the previous node and to traverse the list till we reach the node to be deleted or till we reach the end of the list. This can be illustrated by an example which consists of a linked list with these nodes and our task is to delete a node with value 80. For that we are assigning two pointers that is tree initially it is null and a current which is pointing to the first node. So every time the current where it is pointing to that node the value of that node is compared with the value of X. If the comparison is equal then we have found the node to be deleted. If the value is not equal then we need to advance the trail and the current pointer to the next node. So here you can see that the value of the node pointed to by the current is not equal to X. For that first we need to advance the trail to the current place and current is moved to the next node. After that is done again the value of current node is now compared with the value of X. Here the comparison is unequal. So trail is moved to the current place and current is moved to the next node. Again the comparison is done and you can see that again it is unequal. So trail is moved to the current place and current is moved to the next node. Again the current node value is compared with the X value. Again the comparison is unequal. So trail is moved to the current node and current is moved to the next node. And after this you can see that the comparison is equal. So here we have found the node to be deleted. The next step is to modify the pointers to delete the node. For that the pointers have to be adjusted for deletion of the node from the existing linked list. Here from the previous slide we can see that we want to delete a node with a value 80 which is pointed by current. Now to delete this node only we need to adjust this pointer in this way that trail of next should now be assigned to current of next. After this you can see that the deleted node is now pointed by the current pointer and the linked list is now updated after deleting the node with value 80. Now there are situations after traversing the linked list the trail is null or the current is null. So we are going to discuss this situation in this. So in the first case suppose trail is null after traversing the linked list consider this linked list. Now we want to delete a node with value 40. Now for that we are assigning two pointers that is trail which is initially null and current is pointing to the first node. After comparison with the current node with value of x we can see that the comparison is equal. For that we want to delete the first node. Only what we need to do is the list should now be marked to list of next. After that is done the first node will now be deleted. So after deletion you can see that the current node which is now pointing to the first node has been deleted and this is the updated list. In another situation you can see that suppose current is null after traversing the linked list. Consider the linked list again. Now we want to delete a node with value 110. Now here also as we have done previously we are assigning two pointers that is trail and current. Trail is now initially assigned to null and current is assigned to the first node. We are traversing the linked list and after some time we can see that trail has reached to the last node and current is now pointing to the null. That indicates that the node with value 110 is not existing. So here it can be indicated node to be deleted is not found. Think an answer. What is the algorithm for deletion of a node which is placed anywhere in the linked list? Pause the video for some time and note down the answer in your book. Algorithm for deletion of a node which is placed anywhere in the single linked list. Input from the user the node to be deleted say X. Initialize the pointers current and trail. Travel the linked list till current is not equal to null and current is not equal to X. If stuck tree is true then advance the trail and current. If trail is equal to null then delete the first node of the linked list. If both trail and current are not equal to null then delete the node pointed to by the current. And if current is equal to null then print the node to be deleted is not found. With this it completes the deletion of a node in a single linked list. These are my references. Thank you.