'Freeing/destroying a singly linked list with a for loop
I know it is possible to free a singly linked list like below:
void freeList(struct n** head)
{
struct n* tmp;
while (*head != NULL)
{
tmp = *head;
*head = (*head)->next;
free(tmp);
}
*head = NULL;
}
The above code works perfectly and can be said to be simple enough for its purpose. Is it, however, possible to achieve the same with a for loop instead of a while loop? I have tried alot of quirks but don't seem to getting anywhere.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
