题解 | #链表中环的入口结点#
链表中环的入口结点
https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
import java.util.*; /* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } } */ public class Solution { public ListNode EntryNodeOfLoop(ListNode pHead) { // 用来保存已经遍历过的节点 Set<ListNode> set = new HashSet<>(); while (pHead != null) { // 当一个节点被重复遍历时,说明链表存在环,且该节点就是环的入口节点 if (set.contains(pHead)) { return pHead; } set.add(pHead); pHead = pHead.next; } // 链表遍历结束未发现环,返回null return null; } }