MCPcopy Create free account
hub / github.com/SR-Sunny-Raj/Hacktoberfest2021-DSA / detectLoop

Function detectLoop

10. Linked List/cpp/DetectLoop.cpp:29–51  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27}
28
29int detectLoop(Node* head)
30{
31 Node *fast_pointer = head;
32 Node *slow_pointer = head;
33
34
35 /*
36 Check for null value for fast_pointer and fast_pointer->next
37 So to avoid segmentation fault error for case in which there
38 is no loop for both even and odd number of elements
39 */
40 while(fast_pointer && fast_pointer->next)
41 {
42 slow_pointer = slow_pointer->next;
43 fast_pointer = fast_pointer->next->next;
44 if(fast_pointer == slow_pointer)
45 {
46 return 1;
47 }
48 }
49
50 return 0;
51}
52
53/* Driver code*/
54int main()

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected