import java.util.HashSet;
import java.util.Map;
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
}
public boolean hasCycle(ListNode head) {
HashSet map = new HashSet<>();
while (head != null){
if (map.contains(head)){
return true;
}
map.add(head);
head = head.next;
}
return false;
}
public boolean hasCycle2(ListNode head) {
if (head == null){
return false;
}
ListNode slow = head;
ListNode last = head;
while (last != null && last.next != null){
last = last.next.next;
slow = slow.next;
if (slow == last){
return true;
}
}
return false;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}