Delete node in singly linked list

How to delete a node in a singly linked list given only the pointer to the node to be deleted? The node is neither the head nor the tail of the linked list.

C++:
01 bool linkListDelete(Node* head, Node* p)
02 {
03     Node* prv=head;
04     if(head==NULL)
05         return false;
06     Node* tmp=head->next;
07     while(tmp!=NULL){
08         if(tmp==p){
09             prv->next=tmp->next;
10             delete p;
11             return true;
12         }
13         prv=tmp;
14         tmp=tmp->next;
15     }
16     return false;
17 }
Advertisements
This entry was posted in Array and linked list. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s