Use Floyd's tortoise-and-hare: advance one pointer by 1 and another by 2 each step. If they ever meet, there's a cycle; if the fast pointer reaches null, there isn't. O(n) time, O(1) space.
Data Structures· asked at Google✓ Added to review
💬
Send Feedback / Bug
Feedback Submitted!
Thank you. Your help keeps Geeky Script running smoothly.