/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode first=head;
ListNode second=head;
if(head == null || head.next == null){
return false;
}
first=first.next;
second=second.next.next;
while(first!=second && first!=null && second!=null){
first=first.next;
if(second.next == null){
return false;
}
second=second.next.next;
}
if(first == null || second == null){
return false;
}
return true;
}