Run Time Error...HELP!!! I'm really exhausted


  • 0
    L

    Can anyone help why I always get run time error for this question? I just wiki the algorithm Floyd's cycle-finding algorithm. I think everything should be OK. But it can't pass the last test which is a very large amount of numbers. This is my code:
    class Solution {
    public:
    ListNode *detectCycle(ListNode *head) {
    if(!head)
    return NULL;

        ListNode *curr1 = head;
        ListNode *curr2 = head;
        ListNode *curr3 = head;
        while(1){
            if((!curr1-> next)||(!curr2->next->next))
                break;
            curr1 = curr1 -> next;
            curr2 = curr2 -> next -> next;
            if(curr1 == curr2){
                while(1){
                    curr3 = curr3 -> next;
                    curr1 = curr1 -> next;
                    if(curr3 == curr1)
                        return curr3;
                    else
                        continue;
                }
            }
        }
        return NULL;
    }
    

    };
    I would really appreciate you if you can help me with that...maybe a meal ;)


  • -1
    F

    //i don't understand your code, but

    while(1){
        if((!curr1-> next)||(!curr2->next->next))         //  here,  curr2->next might be NULL...
            break;                                                       // you should check whether curr2->next==NULL or not
    

    I would debug my program with Visual studio (how to set break points is a very useful skill) if it gets "Run Time Error"


Log in to reply
 

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