Cracking the Coding Interview: Linked Lists – Detect A Cycle

Here’s my solution to the challenge: https://www.hackerrank.com/challenges/ctci-linked-list-cycle

bool has_cycle(Node * head) {
    Node * slow = head;
    Node * fast = head;
    while (fast != NULL && fast - > next != NULL) {
        slow = slow - > next;
        fast = fast - > next - > next;
        if (slow == fast)
            return true;
    }
    return false;
}

All my solutions to HackerRank challenges can be found here: https://github.com/sdulaney/hackerrank

Leave a Comment