In this tutorial, we are going to learn how to perform deletion operations in the doubly linked list.
after completing this tutorial you are able to learn how to
- Delete the first node.
- Delete the only node.
- Delete a node in between the nodes.
- Delete the last node.
Deletion of the first node from the doubly-linked list
as you see we have a doubly-linked list that has four nodes. so after deleting the first node second node become the first node.
so first we store the second node’s reference to the start variable.
and then we set the second node’s previous link part to Null or None. because the previous link part of the first node in the doubly linked list is always None or Null.
so after performing these steps, the first node is deleted.
Delete the only node from the doubly linked list.
if a linked list has only one node then to delete this node we set the start variable to the None.
so now our list becomes an empty list.
Delete a node in between the nodes.
to delete a node between two nodes first we need to find out the reference to the node that we want to delete. as you see to delete the third node first we found a reference p to that node.
and then we store the reference of the node that comes after node p into the next link part of the node that comes before node p.
and then we store the reference of the node that comes before node p into the previous link part of the node that comes after node p.
now after performing these steps now, the third node has been deleted from the doubly linked list.
Delete the last node from the doubly linked list
to delete the last node of the list first we need to find out the reference of the last node.
and then we set the next link part of the node to Null or None that come before node p.
so after performing these steps now the last node of the list is deleted.