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;
}
}