Question: [1] tail connects to node index 0 (C++)

  • 0

    I wanted to try the solution of marked method, but I got stuck in this test case:

    tail connects to node index 0
    no cycle
    tail connects to node index 0

    I couldn't figure out what happened.
    Could anyone help me to clear my blindspot ?

    thank you

    My solution:

      ListNode *detectCycle(ListNode *head) {
            vector<ListNode*> check;
            if (!head || !head->next) return NULL;
            while (!head) {
                if (find(check.begin(), check.end(), head) != check.end()) {
                    return head;
                // Move to next
                if (!head->next) {
                    head = head->next;// point to next one
            return NULL;

  • 1

    i think you can quit if input is [1]
    head->next is NULL for [1].

    the problem is that your while loop condition is wrong.

  • 0

    Oh my god, I think that's a stupid mistake!
    Thank you point out my blind spot!

Log in to reply

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