AC with only one submission


  • 0
    class Solution {
    public:
        ListNode *detectCycle(ListNode *head) {
            ListNode* slow=head, *fast=head;
            while(fast && fast->next){
                slow=slow->next;
                fast=fast->next->next;
                if(slow == fast)    break;
            }
            if(fast==NULL || fast->next==NULL)  return NULL;
            ListNode* result=head;
            while(result != fast){
                result=result->next;
                fast=fast->next;
            }
            return result;
        }
    };

Log in to reply
 

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