How will u find whether a linked list has a loop or not?

Answers were Sorted based on User's Feedback



How will u find whether a linked list has a loop or not?..

Answer / sivaraj

BOOL findloop(struct node *start)
{
struct node *ptr,*ptr1;
ptr=start;
ptr1= start->next;
while(ptr!=NULL && ptr->next!=null && ptr1->next!=null &&
ptr1->next->next!=null)
{
if(ptr==ptr1)
return FALSE;
ptr=ptr->next;
ptr1=ptr1->next->next;
}
return TRUE;
}

Is This Answer Correct ?    16 Yes 5 No

How will u find whether a linked list has a loop or not?..

Answer / sanyam

the approach is to mark the nodes starting from the head.and
follow the links.
in the process if you find a node already marked then there
is loop.

Is This Answer Correct ?    13 Yes 2 No

How will u find whether a linked list has a loop or not?..

Answer / adi

answer 2 is correct

Is This Answer Correct ?    4 Yes 0 No

How will u find whether a linked list has a loop or not?..

Answer / priya

same logic as 3rd answer..

typedef struct node
{ int data,mark; /*mark is all initialised to 0*/
node *next;
}node;

node* findloop(node* start)
{
node *p=start;
while(p)
{
if(p->mark==0)
{
p->mark=1;
p=p->next;
}
else
{
return p;
}
}
return p;
}

Is This Answer Correct ?    3 Yes 3 No

How will u find whether a linked list has a loop or not?..

Answer / karthik

I guess the return shud b TRUE in the if(ptr==ptr1) block
since that's the oly condition for presence of a loop in the
LL.. and return shud b a false after d while loop!!!

Is This Answer Correct ?    0 Yes 0 No

How will u find whether a linked list has a loop or not?..

Answer / zameer arif

You are right. In the loop, return should be true. outside
the loop, false.

Is This Answer Correct ?    0 Yes 0 No

How will u find whether a linked list has a loop or not?..

Answer / anurag srivastava

void lopp( node **start)
{
node *temp,*newnode;
temp=*start;
while(*temp && !(temp->next==start))
{ temp=temp->next; }
if(!(*temp)) printf("Non loop");
else printf("Loop");
}

Is This Answer Correct ?    3 Yes 5 No

How will u find whether a linked list has a loop or not?..

Answer / s.v.prasad reddy

findloop(struct node *start)
{
struct node *ptr,*ptr1;
start=ptr;

while(ptr!=NULL)
{
ptr1=ptr->next;

while(ptr1!=NULL)
{
if(ptr1->link==ptr->link)
{
printf("\nLoop found at %d node",ptr->data);
exit(0);
}
ptr1=ptr1->link;
}
ptr=ptr->link;
}
printf("\nNo Loop found in list");
}

Is This Answer Correct ?    9 Yes 17 No

Post New Answer

More C Code Interview Questions

main() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16

4 Answers   HCL,


#include<stdio.h> main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); }

2 Answers  


how can i cast a char type array to an int type array

2 Answers  


void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); }

1 Answers   Honeywell,


main() { struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

1 Answers  






write the function. if all the character in string B appear in string A, return true, otherwise return false.

11 Answers   Google,


#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); }

3 Answers   Hexaware,


main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }

1 Answers  


You are given any character string. Find the number of sets of vowels that come in the order of aeiou in the given string. For eg., let the given string be DIPLOMATIC. The answer returned must be "The number of sets is 2" and "The sets are "IO and AI". Vowels that form a singleton set must be neglected. Try to post the program executable in gcc or g++ or in java.

3 Answers  


#include <stdio.h> int main(void) { int a=4, b=2; a=b<<a+b>>2 ; printf("%d",a); return 0; }

0 Answers   Student,


#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

2 Answers  


Write a routine that prints out a 2-D array in spiral order

3 Answers   Microsoft,


Categories