It is helpful to draw a picture of the linked list. What you are doing here is freeing the second item in the list and then pointing head to the location which you just freed. This is sure to never free the first item in the list, and it is not guaranteed to free the following items either. I think that what you intend to do is to free head rather than free next.
I would suggest a slightly different approach: void deleteList(nodePtr &head) { while(head) { nodePtr tmp = head; head = head->next; delete tmp; } } Why? Because it never passes through a state where head is pointing to a node which has already been deallocated. Don On Apr 9, 2:22 pm, rahul sharma <rahul23111...@gmail.com> wrote: > sory..following is correct code > void deleteList(struct node** head) > { > /* deref head_ref to get the real head */ > struct node* next; > > while (*head != NULL) > { > next = *head->next; > free(next); > *head = next; > } > > > > > > > > } > On Tue, Apr 9, 2013 at 9:27 PM, Don <dondod...@gmail.com> wrote: > > "head" is not even declared, so I doubt that it would compile. > > I believe that you want to free head, not next. > > > On Apr 9, 11:31 am, rahul sharma <rahul23111...@gmail.com> wrote: > > > Is the following code correct for linked list deletion or i need to copy > > > head in some tem. pointer and dlete and theninitialize head to NULLL.Plz > > > comment > > > void deleteList(struct node** head_ref) > > > { > > > /* deref head_ref to get the real head */ > > > struct node* next; > > > > while (*head != NULL) > > > { > > > next = *head->next; > > > free(next); > > > *head = next; > > > } > > > > } > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to algogeeks+unsubscr...@googlegroups.com. > > For more options, visithttps://groups.google.com/groups/opt_out. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.