/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
import java.util.*;
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode temp=head;
Set<ListNode> node=new HashSet<ListNode>();
node.add(head);
while(temp!=null){
temp=temp.next;
if(node.contains(temp))
return true;
node.add(temp);
}
return false;
}
}