slow
and fast
, both pointing to the head of the linked list.slow
pointer by one step and fast
pointer by two steps in each iteration.fast
pointer reaches the end of the list (i.e., fast
is null
or fast.next
is null
), there is no cycle, and return false
.slow
pointer and the fast
pointer meet (i.e., slow
== fast
), a cycle is detected, and return true
.