Cycle Detection in a Linked List | Data Structure
Problem: Our task is to determine is there any loop/cycle in a Linked List or not?
Required Knowledge: Singly Linked List | Data Structure
Explanation:
There is a "slow" pointer that moves one node at a time. There is a "fast" pointer that moves twice as fast, two nodes at a time.
A visualization as slow and fast pointers move through the linked list with 10 nodes:
1: |sf--------|
2: |-s-f------|
3: |--s--f----|
4: |---s---f--|
5: |----s----f|
At this point one of two things is true:
1) the linked list does not loop (checked with fast != null && fast.next != null && slow!=null
) or
2) it does loop. Let's continue visualization assuming it does loop:
6: |-f----s---|
7: |---f---s--|
8: |-----f--s-|
9: |-------f-s|
10: s == f
If the linked list is not looped, the fast pointer finishes the race at O(n/2) time; we can remove the constant and call it O(n). If the linked list does loop, the slow pointer moves through the whole linked list and eventually equals the faster pointer at O(n) time.
CODE in C++:
No comments