Performance
You have implemented Floyd’s Cycle-Finding Algorithm which adheres to \$0(1)\$ storage space. An alternative exists Brent’s Cycle Detection Algorithm which uses the same storage space. Check out this review on Computer Science SE for a comparison. It appears in general, Brent's algorithm is faster.
According to Brent's paper, the complexity of Floyd's algorithm is between
3max(m,n)and3(m+n), and that of Brent's is at most2max(m,n)+n, which is always better than3max(m,n).
courtesy of Yuval Filmus' answer at CS
Style Guidelines
- use
varto declare a variable, specially when the type can be inferred from code - use a separate line for declaring attributes on top of members
- use a white space after a method name and the opening parenthesis
- use a white space after the while statement
- use white space around operators (
!=) - remove redundant nested else branches if the if branch always performs a return