64ms C solution


  • 0
    V

    '''
    struct ListNode *detectCycle(struct ListNode *head) {
    struct ListNode *tmp;
    struct ListNode *h = head;
    if(head != NULL){
    tmp = head->next;
    while(tmp!=NULL&&head!=NULL)
    {
    if(tmp == head)
    break;
    else{
    tmp = tmp->next;
    if(tmp)
    tmp = tmp->next;
    head = head->next;
    }
    }
    }
    if(tmp != head)
    return NULL;
    else{
    int length = 1;
    tmp = tmp->next;
    while(tmp!=head){
    tmp = tmp->next;
    length++;
    }
    for(;h!=NULL;h = h->next){
    int i = length;
    while(i--){
    if(tmp == h)
    return tmp;
    else
    tmp = tmp->next;
    }
    }
    }
    return NULL;
    }
    '''


Log in to reply
 

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