How do you detect a cycle in a linked list?

Technical
Medium
Infosys
78.1K views

This question asks for an algorithm to determine if a singly linked list contains a cycle. It is a standard problem testing pointer manipulation skills.

Why Interviewers Ask This

Detecting cycles is essential for preventing infinite loops in systems. Interviewers assess your knowledge of Floyd's Cycle-Finding Algorithm (Tortoise and Hare). It tests your ability to manipulate pointers without extra space and understand the mathematical properties of cycle detection.

How to Answer This Question

Introduce the two-pointer approach where one moves twice as fast as the other. Explain that if there is a cycle, the fast pointer will eventually catch up to the slow pointer. If the fast pointer reaches the end, no cycle exists. Mention how to find the start of the cycle if needed. Avoid suggesting hash sets unless discussing space trade-offs.

Key Points to Cover

  • Two-pointer technique
  • Fast and slow movement
  • Meeting point indicates cycle
  • O(1) space complexity

Sample Answer

I would use Floyd's Cycle-Finding Algorithm with two pointers, slow and fast. The slow pointer moves one step at a time, while the fast pointer moves two steps. If a cycle exists, the fast pointer will eventually meet th…

Common Mistakes to Avoid

  • Infinite loop due to incorrect pointer update
  • Confusing null termination with cycle
  • Not handling single-node lists

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 165 Technical questionsBrowse all 100 Infosys questions