linked list deletion ?
Posted on 2003-03-06
I have seen a few different ways that people free up the memory for nodes in a linked list. There is one way that I have seen that is confusing to me and I would appreciate an explanation of how it works:
Having a dummy head node that points to the next node and so on:
Node * pHead = new Node;
pHead->next = new Node;
pHead->next->next = new Node;
// ....to free up the memory for all of the Nodes in the whole list
It seems to me that saying "delete pHead;" should just deallocate memory for the Node pHead points to, not get rid of the memory for the rest of them. How does this work?
I have seen people call this a recursive solution, so it might not be good for a big list because of memory issues?
Thanks for your help