Got a question in my exam.
Write a function DeleteList
() that takes a list, deallocates all of its memory and sets its
head pointer to NULL (the empty list).
Solution was given as
void DeleteList(struct node** headRef) {
struct node* current = *headRef;
struct node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
*headRef = NULL;
}
My solution :
void DeleteList(struct node** headRef) {
struct node* current = *headRef;
while (current != NULL) {
*headRef = *headRef->next;
free(current);
current = * headRef;
}
free(current);
*headRef = NULL;
}
Is this correct approach? Thanks,