Output node is not in the linked list


  • 1
    F
    class Solution {
    public:
    bool checkCycle(ListNode* head) {
        if (head == NULL)
            return false;
        ListNode *p1 = head, *p2 = head;
        while (p1 != NULL && p2 != NULL && p2->next != NULL) {
            p1 = p1->next;
            p2 = p2->next->next;
            if (p1 == p2)
                break;
        }
        
        if (p1 == NULL || p2 == NULL || p2->next == NULL) {
            return false;
        }
        
        return true;
    }
    ListNode *detectCycle(ListNode *head) {
        if (!checkCycle(head))
            return NULL;
        ListNode* p = head;
        while (p->next != NULL) {
            ListNode* tmp = p->next;
            p->next = NULL;
            p = tmp;
        }
        return p;
    }
    };
    

    This is my code but the result is :

    Input:	{3,2,0,-4}, tail connects to node index 1
    Output:	output node is not in the linked list
    Expected:	tail connects to node index 1
    

    However, it performs well in visual studio 2010. I really don't know what the meaning of "not in the linked list" is!


  • 0
    X
    This post is deleted!

Log in to reply
 

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