Check a Linked List for Loops or Cycles With O(1) Space Complexity
Check if a given linked list has cycles. Try to achieve O(n) runtime with a space complexity of O(1).
If there is a cycle, return true otherwise return false. Consider empty lists as non cyclic.
Examples: 1->2->3->4->1 ==> true 1->2->3->4 ==> false
Try the Floyd Cycle Finding Algorithm, also known as the 'Tortoise & Hare Algorithm'.
In this approach, you maintain two pointers - 'slow' & 'fast' where slow
moves one node at a time and fast moves two nodes at a time. If there is a cycle, these two pointers will inevitably meet! Seriously, they will.