Delete all occurrences of an item in linked list This code takes an item as an argument and deletes all occurrences of the item in the linked list. It works well with my testing. Is there anything that I am missing? Can this code be improved further?

void
LinkedList::DeleteAllOccurences(int key) {
   Node *temp = head;
   Node *prev = head;
   while(temp!=NULL) {
      if(temp-&gt;item == key){
         if(temp == head) {
            head = temp-&gt;next;
            delete temp;
            temp = head;
         } else {
            prev-&gt;next = temp-&gt;next;
            delete temp;
            temp = prev-&gt;next;
         }
      } else {
         prev = temp;
         temp = temp-&gt;next;
      }
   }
   return;
}
Yes, your code can be improved by not handling the head as a special case. Yes, that means using a double-pointer, but the structure gets much more straight-forward and shorter:

void LinkedList::DeleteAllOccurences(int key) {
    Node** p = &amp;head;
    while(*p)
        if((*p)-&gt;item == key) {
            Node* tmp = (*p)-&gt;next;
            delete *p;
            *p = tmp;
        } else
            p = &amp;(*p)-&gt;next;
}