[C]Recursive method runtime-error


  • 0
    J

    I try to use recursive method to solve this question,but I get Runtime Error!
    I know the runtime may be very slow,but I still want to solve this question by recursion.
    Can anyone give me some advice about the code below?

    #define node_NUM 1000
    
    void *addr_array[node_NUM];//record the address of node
    int _index = 0;
    
    int findAddr(struct ListNode *temp){//0 for not found;1 for found it!
    for(int i =0;i<_index;i++){
        if( temp == addr_array[i]){
            return 1;
        }
    }
    addr_array[_index] = temp;
    _index++;
    return 0;
    }
    
    struct ListNode *detectCycle(struct ListNode *head) {
        if(head == NULL){
            return NULL;
        }
    
        if(head->next != NULL){
            if(findAddr(head)){
                return head;
            }
            return detectCycle(head->next);
        }
    return NULL;
    }

Log in to reply
 

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