Interview Questions and Answers :: Wipro
Home > Experience Archives > Wipro > Interview Question Set 1 > Discussion
5. Write a program to find the loop in a single linklist.
Answer:
/* pass the head reference to detectLoop */
int detectLoop(struct node *list)
{
struct node *slowp = list, *fastp = list;
while (slowp && fastp && fastp->next)
{
slowp = slowp->next;
fastp = fastp->next->next;
if (slow_p == fast_p)
{
printf("\r\nLoop detected in given linked list.\r\n");
return 1;
}
}
return 0;
}
Ashwani
16 Aug, 2016 3:07 PM
/* pass the head reference to detectLoop */
int detectLoop(struct node *list)
{
struct node *slowp = list, *fastp = list;
while (slowp && fastp && fastp->next)
{
slowp = slowp->next;
fastp = fastp->next->next;
if (slow_p == fast_p)
{
printf("\r\nLoop detected in given linked list.\r\n");
return 1;
}
}
return 0;
}
int detectLoop(struct node *list)
{
struct node *slowp = list, *fastp = list;
while (slowp && fastp && fastp->next)
{
slowp = slowp->next;
fastp = fastp->next->next;
if (slow_p == fast_p)
{
printf("\r\nLoop detected in given linked list.\r\n");
return 1;
}
}
return 0;
}
Report Error
Report Error
Please Login First Click Here