Doubt in erase function

Just wanted to know if my erase function is right?

void erase(string key) {
            int idx=hashFn(key);

            Node*temp=table[idx];
            Node*prev=NULL;

            while(temp){
                if(temp->key==key){

                    if(prev)
                        prev->next=temp->next;
                    else
                        table[idx]=temp->next;

                    delete temp;
                }

                prev=temp;
                temp=temp->next;
            }
        }

Or before deleting temp, I have to do temp->next = NULL ?

this is also correct
no need to make temp->next = null
because temp is just going to delete

1 Like

yeah, thanks a lot. mark this close

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.