problem id:--http://practice.geeksforgeeks.org/problems/detect-loop-in-linked-list/1
code:--
int detectloop(struct node *list){
struct node *p,*q;
p=list,q=list;
int flag=1;
while(flag==1)
{
if(p==NULL||p->next==NULL ||q==NULL||(q->next)==NULL||(q->next)==NULL)
{
flag=0;
break;
}
p=p->next;
q=(q->next)->next;
//printf("%d %d \n",p->data,q->data);
if(p==q)
break;
}
if(flag==1)
return 1;
else
return 0;
}
code:--
int detectloop(struct node *list){
struct node *p,*q;
p=list,q=list;
int flag=1;
while(flag==1)
{
if(p==NULL||p->next==NULL ||q==NULL||(q->next)==NULL||(q->next)==NULL)
{
flag=0;
break;
}
p=p->next;
q=(q->next)->next;
//printf("%d %d \n",p->data,q->data);
if(p==q)
break;
}
if(flag==1)
return 1;
else
return 0;
}
No comments:
Post a Comment