2 ms·
It's not difficult. Keep a pointer to a recently-visited node. After visiting N nodes, update the pointer to the current node, but also double N. If the pointer
by mdavidn 4y ago
It's not difficult. Keep a pointer to a recently-visited node. After visiting N nodes, update the pointer to the current node, but also double N. If the pointer ever matches the next node, the list has a cycle. The doubling of N ensures that even large cycles will eventually be detected.