Deviopath › Go

Linked List Cycle

Hard · Algorithms

A list is given as next indexes: next[i] is the index of the node after i, or -1 for none. Node 0 is the head. Return whether following next from 0 loops.

Write HasCycle(next). Use Floyd’s tortoise and hare so you do not allocate a set — but a set is also accepted.

Next: Count Islands