C Program to Find Cycle in a Linked List

Write a C program to detect a loop in a linked list.

How to check whether a linked list contains a cycle.

Given a Singly list, we have to find whether given linked list contains a cycle. A loop in a linked list means there is no tail node in a linked list, every node of link list is pointing to some other node of linked list.

Method 1 : Fast and Slow pointer method.

Algorithm to detect cycle in a linked list
Let "head" be the head pointer of given linked list.

Let, "slow" and "fast" be two node pointers pointing to the head node of linked list.

In every iteration, the "slow" pointer moves ahead by one node(slow = slow->next;) whereas "fast" pointer moves two nodes at a time(fast = fast->next->next;).

If linked list contains a loop, "slow" and "fast" pointers will eventually meet at the same node, thus indicating that the linked list contains a loop.

If pointers do not meet then linked list doesn’t have loop.

This algorithm is known as Floyd’s Cycle-Finding Algorithm

In this program, we will use a user defined function "findloop" which takes a pointer to the head node of linked list as input from user and check whether linked list contains a cycle or not by implementing above algorithm.