Why Time Limited Exceeded?


  • 1
    4

    my code use two points:

        public ListNode detectCycle(ListNode head) {
    	if(head==null||head.next==null) return null;
    	ListNode low =head;
    	ListNode fast=head;
    	while(low!=null&&fast!=null){
    		low=low.next;
    		fast= fast.next!=null ? fast.next.next : null;
    		if(low==fast) break;
    	}
    	if(low==null||fast==null) return null;
    	low=head;
    	while(low!=fast){
    		low=low.next;
    		fast=fast.next;
    	}
    	return fast;
    }

  • 0
    L

    add else around
    low=head;
    while(low!=fast){
    low=low.next;
    fast=fast.next;
    }
    return fast;
    then it will be accepted .but i do not why it can happen


  • 0
    W

    No, it doesn't work, I tried....


Log in to reply
 

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