Simple pointer question...

Is the following code valid for removing the first node of a linked list?

1
2
3
4
Node *temp = head;
head= head->next;
delete temp;
temp= NULL;


On line 2, does head= head->next also assign temp to head->next? Or does temp still point to the original head?

thank you!
It's fine, but you don't need to assign NULL to temp after you delete it. I'm assuming it will go out of scope almost immediately, right? Then anything else you do with the pointer after deleting it is irrelevant.
I see, I always thought it was just good practice to assign a pointer to NULL. Thanks!! Solved.
It only makes sense if the pointer will continue to exist later. For example, if the pointer is a class member, and it's being deleted somewhere other than the destructor.
Topic archived. No new replies allowed.