I\'m trying to find this algorithm on C++ in .NET but can\'t, I found this one:
// Best solution
function boolean hasLoop(Node startNode){
Node slowNode = Node
The idea in the code you've found seems fine. Two fast iterators are used for convenience (although I'm positive such kind of 'convenience', like putting a lot of 'action' in the condition of while
loop, should be avoided). You can rewrite it in more readable way with one variable:
while (fastNode && fastNode.next()) {
if (fastNode.next() == slowNode || fastNode.next().next() == slowNode) {
return true;
}
fastNode = fastNode.next().next();
slowNode = slowNode.next();
}