I get runtime error while checking cycle in linked list

  • 0

    hi everyone,

    here is my code,

     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
    bool hasCycle(struct ListNode *head) {
        struct ListNode *p1;
        struct ListNode *p2;
        p1 = head;
        p2 = head;
        while(p1 && p2){
            p1 = p1->next;
            p2 = p2->next;
                p2 = p2->next;
                return false;
                return true;
        return false;

    however, I get some runtime error like below,

    Last executed input:	{}, no cycle

    I really don't know why I get this error, could anyone can help me? Thanks a lot!

Log in to reply

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