Cycle Detection in Linked List (Floyd's Algorithm)
M · mediumPracticelinked-list
Problem
Given a linked list, detect if there is a cycle. Print “Cycle Detected” or “No Cycle”.
Example
Input
1 -> 2 -> 3 -> 4 -> 2 (cycle back to node 2)
Output
Cycle Detected
Slow pointer moves 1 step, fast pointer moves 2 steps. If they meet → cycle. If fast=NULL → no cycle.