How to detect a loop (cycle) in a linked list

Traversing linked list is easy but what would happen if there is a cycle in the linked list?It will just fall into an infinity loop if traversing is implemented without a cycle prevention code.Thankfully, there is already a known algorithm for this kind of problem. How to detect if a cycle exists? Suppose there is a linked list that has a cycle like below.Now the question is how to detect the cycle. Let’s first think about what would happen if there is a cycle. Iterating linked list typically happens in a while loop like these steps. Check if the node READ MORE