
Master Coding Interviews : Part 3 ( Fast & Slow Pointers Pattern )
In Part 1 we saw how a sliding window collapses a nested loop into a single pass over an array. In Part 2 we used two pointers moving toward each other — or side by side — to restructure and search sorted arrays in O(n) time and O(1) space. Today's pattern shares that same O(1) space philosophy, but it lives in a different world: linked lists . Meet the Fast & Slow Pointers pattern — also known as Floyd's Cycle Detection Algorithm. What is the Fast & Slow Pointers Pattern? The concept comes from a simple real-world intuition: imagine two runners on a circular track. One runs twice as fast as the other. If the track loops back on itself, the fast runner will eventually lap the slower one — they are guaranteed to meet. If the track is a straight line with a finish line, the fast runner simply gets there first and stops. We apply exactly this logic to linked lists: The slow pointer advances one node at a time The fast pointer advances two nodes at a time If the list has a cycle, they will
Continue reading on Dev.to Webdev
Opens in a new tab


