How to delete whole linked list?

Jun 25, 2018 at 5:50pm
Can't find information, can only find how to delete at certain place.
Jun 25, 2018 at 6:02pm
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 Jun 25, 2018 at 6:03pm
Jun 25, 2018 at 6:06pm
Can't find information, can only find how to delete at certain place.


Delete the whole list, or just one item in the list?
Jun 25, 2018 at 6:14pm
whole
Jun 25, 2018 at 7:06pm
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 Jun 25, 2018 at 7:07pm
Topic archived. No new replies allowed.