Use Floyd’s tortoise and hare cycle detection.
Move slow by 1 and fast by 2. If there’s a cycle they eventually meet; if fast reaches null there’s no cycle.
In a cycle, the faster pointer gains on the slower pointer by 1 per step, guaranteeing a meeting.
Use Floyd’s tortoise and hare cycle detection.
Move slow by 1 and fast by 2. If there’s a cycle they eventually meet; if fast reaches null there’s no cycle.
In a cycle, the faster pointer gains on the slower pointer by 1 per step, guaranteeing a meeting.