Interview Questions and Answers :: Hewlett Packard Enterprise
Home > Experience Archives > Hewlett Packard Enterprise > Interview Question Set 1 > Discussion
3. Write a program to find the loop in sigle linked list.
Answer:
bool loopInLinkedList(Node *list)
{
Node *slow_p, *fast_p;slow_p = list;
fast_p = list;
while (slow_p && fast_p && fast_p->next)
{
slow_p = slow_p->next;
fast_p = fast_p->next->next;
if (slow_p == fast_p)
return TRUE;
}
return FALSE;
}
Amit
10 Aug, 2018 5:14 PM
bool loopInLinkedList(Node *list)
{
Node *slow_p, *fast_p;slow_p = list;
fast_p = list;
while (slow_p && fast_p && fast_p->next)
{
slow_p = slow_p->next;
fast_p = fast_p->next->next;
if (slow_p == fast_p)
return TRUE;
}
return FALSE;
}
Report Error
Report Error
Please Login First Click Here