Runtime Error Last executed input: {}, no cycle


  • 0
    Z
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            if(head == NULL)
                return false;
            ListNode *p, *q;
            p = q = head;
            while(p!=NULL && q!=NULL && q->next!=NULL )
            {
                p = p->next;
                q = q->next->next;
                if(p == q)
                    return true;
            }
            return false;
        }
    };
    

    The c++ solution is accepted, but the nearly same code in c is failed.

    bool hasCycle(struct ListNode *head) {
        if(head == NULL)
            return false;
        struct ListNode *p, *q;
        p = q = head;
        while(p != NULL && q != NULL && q -> next != NULL )
        {
            p = p->next;
            q = q->next->next;
            if(p == q)
                return true;
        }
        return false;
    }
    Submission Result: Runtime Error
        Last executed input:	{}, no cycle
    

    I don't know whether the different programming languages have a common test data.
    I guess there is some wrong with test data in C, meanwhile, I find a solution accepted before in discussion in C and submit it , but it feedback with the same error as above.
    the accepted solution before: https://leetcode.com/discuss/31305/my-5-ms-c-solution

    bool hasCycle(struct ListNode *head) {
    
    
        if( head == NULL){
            return false;
        }
    
        bool hasCycleResult = false;
        struct ListNode * slow = head;
        struct ListNode * fast = head->next;
    
    
        while(fast != NULL ){
    
            if(slow == fast){
                hasCycleResult = true;
                break;
            }
    
            slow = slow->next ;
    
            fast = fast->next;
    
            if(fast != NULL){
                fast = fast->next;
            }
        }
    
        return hasCycleResult;
    }
    

    so maybe the test data in C has been strengthened, or the C compiler or again parameters have been changed.
    I don't know the reason and confused about it.


Log in to reply
 

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