How to delete whole linked list?

Can't find information, can only find how to delete at certain place.
crudely:

while(head) //repeat until empty.
{
tmp = head->next. //save the rest of the list. this may be null, and that is OK, its the end of the loop/list
delete head. //destroy the top node.
head = tmp; //the list is now the same as before minus the top node. this may also be null/empty list, the while will catch that.
}
Last edited on
Can't find information, can only find how to delete at certain place.


Delete the whole list, or just one item in the list?
whole
I gave you the algorithm, is that sufficient for you?

BTW: It should be just a repeated call to the logic you already found for 'delete head' special case of 'delete any node'.

Last edited on
Topic archived. No new replies allowed.