My java solution


  • 0
    C
     public class Solution{
       public ListNode detectCycle(ListNode head) {
    	if(head == null)return null;
    	
    	ListNode first = head;
    	ListNode second = head;
    			
    	if(head == head.next)
    		return head;
    	
    	while((first != null) &&(second !=null)&& (second.next != null))
    	{
    		first=first.next;
    		second=second.next.next;
    		
    		if(first == second)
    		{
    			return findPoint(head,first);
    		}
    		
    	}
    	
    	return null;
    	
    }
    
    public ListNode findPoint(ListNode head,ListNode meetpoint)
    {
    	ListNode current = head;
    	while(current != meetpoint)
    	{
    		current=current.next;
    		meetpoint=meetpoint.next;
    	}
    	return current;
    }
    

    }


Log in to reply
 

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