How to find the case of deletion of the node in Linked List

Hello TA,
I got the position of the node, I mean the address of the node in the linked list where that particular key is lying, but how will I know that is it at head or tail and then which case to follow up to delete it.
The code I have written till now is:

void erase(string key){
int idx = hashFun(key);
Node *ptr = table[idx];
while(ptr!=NULL){
if(ptr->key==key){
break;
}
ptr = ptr->next;
}
// What is the case of deletion of Linked List
}

@hargovind hey usually apko head pointer pta hota hai ,in your case jo sabse pehla wala hai woh head hai,so now two cases are there in deletion of node in linked list ,if deleted node is head or any other node in linked list ,so you have to traverse till deleting node and also get the pointer to previous of deleting node if deleting node is not head ,then do previous of deleting node->next=deleting node->next and for head case just do head=head->next and delete first node,in this way you can delete node.

1 Like

@rishabhmahajan546 Thanks a Lot. I implemented that function!

@hargovind no problem

1 Like