Home | Projects | Notes > Problem Solving > LC - E - 141. Linked List Cycle (unordered_set, Floyd's Cycle Detection Algorithm - Two Pointers)

LC - E - 141. Linked List Cycle (unordered_set, Floyd's Cycle Detection Algorithm - Two Pointers)

 

Solutions in C++

Solution 1

This solution uses STL unordered_set container.

Complexity Analysis:

Solution:

 

Solution 2

This solution uses the Floyd's Cycle Detection Algorithm; the Two Pointers approach. A fast pointer and a slow pointer are used to determine if there exists a cycle in the loop.

If a loop exists in the linked list, the fast and slow pointers are bound to meet at some point.

Complexity Analysis:

Solution: