Given a linked list, check if the the linked list has loop or not. Below diagram shows a linked list with a loop.

C Algorithm - Write a program function to detect loop in a linked list

Following are different ways of doing this
Use Hashing:
Traverse the list one by one and keep putting the node addresses in a Hash Table. At any point, if NULL is reached then return false and if next of current node points to any of the previously stored nodes in Hash then return true.

[ad type=”banner”]

Mark Visited Nodes:
This solution requires modifications to basic linked list data structure.  Have a visited flag with each node.  Traverse the linked list and keep marking visited nodes.  If you see a visited node again then there is a loop. This solution works in O(n) but requires additional information with each node.
A variation of this solution that doesn’t require modification to basic data structure can be implemented using hash.  Just store the addresses of visited nodes in a hash and if you see an address that already exists in hash then there is a loop.

Floyd’s Cycle-Finding Algorithm:
This is the fastest method. Traverse linked list using two pointers.  Move one pointer by one and other pointer by two.  If these pointers meet at some node then there is a loop.  If pointers do not meet then linked list doesn’t have loop.

C Programming:

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20to%20detect%20loop%20in%20a%20linked%20list%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20Link%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node*%20next%3B%0A%7D%3B%0A%20%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0Aint%20detectloop(struct%20node%20*list)%0A%7B%0A%20%20%20%20struct%20node%20%20*slow_p%20%3D%20list%2C%20*fast_p%20%3D%20list%3B%0A%20%20%0A%20%20%20%20while%20(slow_p%20%26%26%20fast_p%20%26%26%20fast_p-%3Enext%20)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20slow_p%20%3D%20slow_p-%3Enext%3B%0A%20%20%20%20%20%20%20%20fast_p%20%20%3D%20fast_p-%3Enext-%3Enext%3B%0A%20%20%20%20%20%20%20%20if%20(slow_p%20%3D%3D%20fast_p)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20printf(%22Found%20Loop%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20return%200%3B%0A%7D%0A%20%0A%2F*%20Drier%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%20%20%20%20struct%20node*%20head%20%3D%20NULL%3B%0A%20%0A%20%20%20%20push(%26head%2C%2020)%3B%0A%20%20%20%20push(%26head%2C%204)%3B%0A%20%20%20%20push(%26head%2C%2015)%3B%0A%20%20%20%20push(%26head%2C%2010)%3B%0A%20%0A%20%20%20%20%2F*%20Create%20a%20loop%20for%20testing%20*%2F%0A%20%20%20%20head-%3Enext-%3Enext-%3Enext-%3Enext%20%3D%20head%3B%0A%20%20%20%20detectloop(head)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

Found loop

Time Complexity: O(n)
Auxiliary Space: O(1)

[ad type=”banner”]