I am learning Linked list data structure in C++, So I wrote this code which deletes a node with specific data value(key).
void deleteNodeK(Node** headref, int key){
Node* temp = *headref;
Node* prev = NULL;
while(temp->data!=key && temp!=NULL){
prev = temp;
temp = temp->next;
}
if(temp==NULL){
return;
}
prev->next = temp->next;
delete temp; //or free(temp);
}
As you can see the last line says delete temp;
but some websites have the same code but with free(temp)
.
Which one of them is correct or better to use in C++?