Delete a Linked List node

Delete a Linked List node

In my previous article, I have discussed the introduction of linked list and linked list insertion. In this article, we will see how to delete a node from the existing linked list.

Delete a Node:

There are three ways to delete a node from the linked list. It depends on the user requirements.

  1. Delete a node from the beginning.
  2. DeleteĀ a node from the middle.
  3. Delete a node from the end.

Note: in the previous article I have already discussed, how to create a linked list so please if you are not aware of the linked list then please see my previous article.


Generic steps to delete a node

Here I am discussing some generic steps to delete a node from the linked list.These steps depend on user implementation.

  • Find the previous node of a node which you want to delete.
  • Remove that node.
  • Reconnect the linked list.
  • Free the allocated memory of the removed node.
  • Update the link to the beginning (if necessary).

Note: The order in which we perform these steps will depend on how we implement the deletion operation.

Delete a node from the beginning

When deleting the node at the beginning of the linked list then there is no need of relinking of nodes, because there is no node available in the back of the first node.
For example, removing a node from the beginning:

www.aticleworld.com

Example source code:

OutPut:



Delete a node from a certain position

First, find the preceding node of a node which you want to remove after that just skip over the node which being removed.
For example, removing the 2nd node of the linked list.

Example source code:

OutPut:

Delete a node from the end.

In which we will delete the last node of the list. The preceding node of the last node become the new last node of the linked list.
For example, removing the last node.



Example source code:

OutPut:



Leave a Reply