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

Is the following code legal? typedef struct a aType; struct a { int x; aType *b; };

1 Answers  


Write a single line c expression to delete a,b,c from aabbcc

2 Answers   Microsoft,


What is the match merge ? compare data step match merge with proc sql merge - how many types are there ? data step vs proc sql

0 Answers  


how to return a multiple value from a function?

5 Answers   Wipro,


How do I write a program to print proper subset of given string . Eg :input: abc output:{},{a},{b},{c},{a,b},{a,c},{b,c}, {a,b,c}.I desperately need this program please mail me to saravana6m@gmail.com

11 Answers   Deshaw, Infosys,






const int perplexed = 2; #define perplexed 3 main() { #ifdef perplexed #undef perplexed #define perplexed 4 #endif printf("%d",perplexed); } a. 0 b. 2 c. 4 d. none of the above

1 Answers   emc2, HCL,


main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); }

4 Answers   CSC,


main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }

1 Answers  


What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }

1 Answers  


void main() { int *i = 0x400; // i points to the address 400 *i = 0; // set the value of memory location pointed by i; }

2 Answers  


write a c program to Reverse a given string using string function and also without string function

1 Answers  


main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } }

1 Answers  


Categories