I'm working through some pointer/linked-list problems. One of the problems is to delete all nodes in a list and point the head to NULL.
My solution differs from the given answer. I'm new to this, so I'm having trouble figuring out if and why mine doesn't work. The main problem I'm having is trying to understand what the result of free(*headRef); is, and if *headRef can share a different pointee after that.
My thinking is: because I have compliment point to the next node, I can free *headRef which is pointing at the first node (or, more generally, the node before the one compliment is pointing to). Then, I can point *headRef to compliment and the process can continue.
Here's my code:
void DeleteList(struct node** headRef){
struct node* compliment = *headRef;
while (compliment != NULL){
compliment = compliment->next;
free(*headRef);
*headRef = compliment;
}
*headRef = NULL;
}
Assume each node carries two attributes: an int and a ->next pointer.
headRefisNULL. The*headRef = NULL;after thewhileisn't necessary. Otherwise, looks okay. - Daniel FischerNULLpointer into this function. I'm guessing the intended usage isDeleteList(&pointerToHeadOfList). - templatetypedef