JunyiCode
4/14/2020 - 3:00 AM

141. Linked List Cycle

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null || head.next == null)   
            return false;
        ListNode slow = head.next;
        ListNode fast = head.next.next;
        
        while(slow != fast) {
            if(slow != null && fast != null && fast.next != null) {
                slow = slow.next;
                fast = fast.next.next;
            } else {
                return false;
            }
        }
        
        return true;
    }
}