Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
430 B
1
Indexable
Never
bool hasCycle(struct ListNode *head) {
    if (!head)
    {
        return false;
    }
    struct ListNode *fastTravel = head;
    struct ListNode *slowTravel = head;

    while(fastTravel && fastTravel->next)
    {
        fastTravel = fastTravel->next->next;
        slowTravel = slowTravel->next;

        if(slowTravel == fastTravel)
        {
            return true;
        }
    }

    return false;
}
Leave a Comment