The algorithm: Initialize slow = head, fast = head While fast and fast.next are not null: - Move slow one step: slow = slow.next - Move fast two steps: fast = fast.next.next - If slow == fast, return true (cycle detected) Return false (fast reached the end, no cycle) Why do they meet? In a cycle, fast gains one node per step on slow. Eventually fast will lap slow.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/linked-lists/linked-list-cycle-solution
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████████████████████████████████████████████████████████████████████████████████████