The most concise, typical two pointers solution

  • 3
    bool hasCycle(ListNode *head) {
        if (!head) return false;
        ListNode *s = head, *f = head->next;
        while (f && f->next) {
            s = s->next;
            f = f->next->next;
            if (s == f) return true;
        return false;

  • 0

    Thanks! I think the idea is Floyd's cycle-finding algorithm.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.