/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
import java.util.HashSet; // 引入 HashSet 类
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
HashSet<ListNode> hashset=new HashSet<>();//记录每一个节点,若有环则会重复出现,直接返回
while(pHead!=null){
if(hashset.contains(pHead)){
return pHead; //有重复节点表示有环
}
hashset.add(pHead);
pHead=pHead.next;
}
return null;//无环返回空
}
}