Using loop to traverse through linked list

Viewed 76631

I was reading about a few basic operations on linked list and I saw two types of loops being used predominantly


struct node {
   int data;
   struct node *next;
}*start=NULL,*tmp;

The first loop was of the form

for(tmp=start;tmp->next!=NULL;tmp=tmp->next);

Using the above loop, now the tmp pointer points towards the last node in the list

The second loop was of the form

tmp=start;
while(tmp!=NULL)
{
     // do something
}

I think that both of them do the same work, but I'm not sure. Is there any difference?

4 Answers
Related