Why that cast is wrong?


  • 0
    S
    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null || head.next.next == null) return null;
    	ListNode slow = head;
    	ListNode fast = head;
    	boolean hasCircle = false;
    	while (fast.next != null && fast.next.next != null) {
    		if (fast == slow) {
    			hasCircle = true;
    			break;
    		}
    		slow = slow.next;
    		fast = fast.next.next;
    	}
    	
    	if (hasCircle) {
    		fast = head;
    		while (slow != fast) {
    			slow = slow.next;
    			fast = fast.next;
    		}
    		return fast;
    	}
    	return null;
    }
    

    I checked for many times,there is NO mistake...

    WHY?


Log in to reply
 

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