Cycle Detection in Linked List (Floyd's Algorithm)

M · mediumPracticelinked-list

Given a linked list, detect if there is a cycle. Print “Cycle Detected” or “No Cycle”.

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.
← Curtain Factory — Max Aqua Curtains in Any BoxDecimal to Binary →
Report an issue with this question