Detect Loop in Linked List

How to detect loop in linked list.

Given a linked list, determine if it has a cycle or loop in it.

For example:

In the below example, the last node (whose value is 9) points to node whose value is 5. The last node of a linked list does not points to a null. It has a cycle.

Detect a loop in a linked list

Detect loop in a linked list