Squash the Bug

FLOYD'S TORTOISE AND HARE

Floyd's tortoise and hare algorithm aims to find a cycle in a linked list. It is a pointer algorithm that uses two pointers moving through the linked list at different speeds.

A cycle, in graph theory, is a sequence of distinct edges whose conjoining vertices are non-empty (non-null) and in which, the first and last vertices are equal.

Given a linked list, and a function to detect a cycle, the hare and tortoise pointers both start out at the head of the linked list. At every iteration, the hare pointer will move twice as fast as the tortoise pointer, such that the distance between the hare and tortoise increases by 1. If there is a cycle in the linked list, both the hare and the tortoise pointers will eventually meet.

The time complexity for this algorithm, in Big O notation, is O(n); whereas, the space complexity is O(1).

In this challenge, your task is to find the bug(s) in the code provided. If you succeed, you stand a chance to interview with Urbanmetry and win a RM50 Shopee voucher!


> when you click on the RUN button, your code status will be reported here.