Deleting a node in Linked List

Written by Ali on. Posted in C++

Hello people and welcome to yet another post of our series on Linked List. We have learnt how to create and traverse a Linked List. We also came to know how to add a node to a particular location in a Linked List. In this post, we’ll learn how to delete a node in a Linked List.

Linked lists are one of the best ways to manage a large number of data. However, during manage some of this data; you may feel the need to delete some of it. Now Linked Lists are a complex yet extremely helpful still, as we have laid emphasis in previous posts, a slight error on your part can lose the information stored in Linked List and send your program crashing. There are 3 instances of deleting a node in Linked List; from the Head, from the middle and from the end of the Linked List. Now let us look at all three instances and see how to delete information.

From the Head:

                Deleting node form the head pointer is one of the tricky methods you’ll tackle in Linked Lists. As you have previously seen, whenever adding a node we’d make a temporary node and then assign it the necessary information and then place it in the appropriate position in the Linked List. When deleting from the Head, you need to send the Head pointer itself. We hadn’t been able to figure out why is this so in the case of first node to be deleted from a Linked list! Using Head pointer directly is a dangerous game to play. You’ll start off with specific information to search against in the Linked List. This is done by traversing the Linked List and finding the value in the List. For the value to be found at the Head, this is bit that should be done.

</p>
<p>//we assume the student struct used previously is used here with</p>
<p>//the linked list When there is no more debt association it changes from  <a href="http://free-credits-report.com/about-credit-agencies/annual-credit-report">annual credit report</a>  to money due to our current laws. already created.</p>
<p>. if(Head-&gt; rollnum == num)</p>
<p>.{</p>
<p>. student * temp = NULL;</p>
<p>. temp = Head;</p>
<p>. Head = Head -&gt; Next;</p>
<p>. temp = NULL;</p>
<p>. delete temp;</p>
<p>. }</p>
<p>

Here, we first store the contents of Head in a temporary pointer, appoint Head to the Next and then delete that temporary pointer.

From the Middle:

                It’s not much of deal when deleting a node from the middle of the list. For that just traverse the list till the value is found, store the contents in a temporary pointer and then delete that pointer; appointing the pointer to its Next. The only difference is the traversing method of the Lined List.

</p>
<p>student * current = Head;</p>
<p>student * temp = NULL;</p>
<p>If(current -&gt; Next -&gt; rollnum == num )</p>
<p>.{</p>
<p>. temp = current -&gt; Next;</p>
<p>. current -&gt; Next = current -&gt; Next -&gt; Next;</p>
<p>. temp = NULL;</p>
<p>. delete temp;</p>
<p>.}</p>
<p>

The code is self-explanatory as it shows how the traversing takes place for the searching of the roll number in the Linked List and thus the deletion.

From the Last:

For the last node, we would have to set two checks to ensure that the node has been found in the Linked List. Take a look at the code below.

</p>
<p>student * current = Head;</p>
<p>student * temp = NULL;</p>
<p>If(current -&gt; Next == NULL &amp;&amp; current -&gt; rollnum == num )</p>
<p>.{</p>
<p>. temp = current;</p>
<p>. current = NULL;</p>
<p>. temp = NULL;</p>
<p>. delete temp;</p>
<p>.}</p>
<p>

Here, we need not to advance the List as the List is already on its last node thus we are just left with deleting the node and setting the pointer as NULL so that we have a check to traverse the Linked List with.

That’s all for now folks, you’ll be hearing soon from us. Till then, stay safe!

Tags: