Thursday, December 3, 2026 | London 10°C · Drizzle
DailyGlimpse

AI

Floyd's Cycle Detection for Linked List Cycle II: Finding the Start Node

RisingBrain 1 min read

In this technical deep dive, we explore the problem of finding the starting node of a cycle in a linked list using Floyd's Cycle Detection algorithm. The algorithm, also known as the tortoise and hare approach, uses two pointers moving at different speeds. We provide a proof for why the algorithm works and implement it in Java, Python, and C++.

The problem assumes a linked list where a tail node may point back to any node in the list, creating a cycle. The goal is to return the node where the cycle begins, or null if there is no cycle.

Floyd's algorithm proceeds in two phases:

  1. Detect the cycle by moving a slow pointer one step and a fast pointer two steps at a time. If they meet, a cycle exists.
  2. Find the start node by resetting one pointer to the head and moving both pointers one step at a time; the meeting point is the cycle's start.

We provide a mathematical proof that the distance from the head to the cycle start (L) equals the distance from the meeting point to the cycle start along the cycle direction (C). Implementations in Java, Python, and C++ are shown, with careful handling of edge cases like a null head or cycle starting at head.

React