A simple accepted solution


  • 0
    S

    public class Solution {
    public ListNode detectCycle(ListNode head) {

    	ListNode temp=null;
    
    	int i=0;
    	if(head==null)
        	return null;
        	else{
        		temp=head;
    	ArrayList<ListNode> al=new ArrayList<ListNode>();
    	al.add(temp);
    	
    	while (true)
    	{
    	
    		if (al.contains(temp.next))
    		{
    			i=1;
    			break;
    		}
    		if(temp.next==null) break;
    		al.add(temp.next);
    		temp=temp.next;
    	}
    	if(i==1)
    		return temp.next;
    	else return null;
        	}
    

    }

    }

Log in to reply
 

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