利用Set去重属性来找到环的入口节点
链表中环的入口结点
http://www.nowcoder.com/questionTerminal/253d2c59ec3e4bc68da16833f79a38e4
利用Set去重属性来找到环的入口节点,代码如下:
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null) {
return pHead;
}
HashSet<ListNode> set = new HashSet<>();
while (pHead != null) {
if (set.add(pHead)) {
pHead = pHead.next;
} else {
return pHead;
}
}
return null;
}
查看12道真题和解析