In this post, we will discuss a simple method to delete a node when we have only the pointer of the node to be deleted and no link to the head or tail node.
Deleting node with the only pointer to that node is one of the famous and very easy interview questions and is asked a lot.
The method we use can be used for both simple or doubly linked list. To delete the given node, we can use the following method
Algorithm:
- Let the node to be deleted be called as the current node.
- Create a temp node to point to the next node.
- Now copy the contents of the temp node to the current node.
- copy the link field of the temp node to the link field of the current node.
- delete or free the memory pointer by the temp node.
C++ Program
Sample input and output to check the program
You might also be interested in
Singly Linked List
Double Linked List
Linked List in Python
Finding Kth element from the End of Linked List
Stack Implementation using Linked List
Queue Implementation using Linked List
Vigenere Cipher Encryption
Check for Anagram Strings
Comments
Post a Comment