Practice mode Data Structures

Which algorithm detects a cycle in a linked list using two pointers that move at different speeds?

Correct answer C. Floyd's tortoise and hare algorithm

Explanation

Floyd's method moves a slow pointer one node per step and a fast pointer two nodes per step. If the list has a cycle the fast pointer eventually laps and meets the slow one; if not, the fast pointer reaches null. It runs in O(n) time with O(1) extra space. Dijkstra and Kruskal are graph algorithms, and Kadane's finds a maximum subarray sum.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.