slow but without extra space inc++


  • 0
    F

    ''' class Solution {
    public:
    ListNode* judge(ListNode* head, ListNode* runner,ListNode* ori_head){
    if((runner == NULL)|| runner->next == NULL){
    return NULL;
    }
    else if(head == runner){
    while(runner != ori_head){
    runner = runner-> next;
    while(head != runner){
    if(ori_head == runner){
    return runner;
    }
    runner = runner->next;
    }
    ori_head = ori_head->next;
    }
    return ori_head;
    }
    else{
    return judge(head->next,runner->next->next,ori_head);
    }
    }
    ListNode *detectCycle(ListNode *head) {
    if((head == NULL) || (head -> next == NULL)){
    return NULL;
    }
    return judge(head, head->next,head);
    }
    };'''


Log in to reply
 

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