JAVA 1ms O(1)space


  • 0
    L

    public class LinkedListCycleII {

    public ListNode detectCycle(ListNode head) {
    
    		if(head==null)
    			return head;
    		ListNode t=head;
    		ListNode r=head.next;
    			while(t!=r)
    			{
    				if(t==null)
    					return null;
    				if(r==null)
    					return null;
    				t=t.next;
    				if(r.next!=null)
    					r=r.next.next;	
    				else
    					return null;
    			}
    			ListNode start=new ListNode(0);
    			start.next=head;
    			t=start;
    			while(t!=r)
    			{
    				t=t.next;
    				r=r.next;		
    			}
    			return t;
    }

Log in to reply
 

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