1、题目描述
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
2、代码实现
import java.util.HashSet;
import java.util.Set;
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead)
{
if (pHead == null) {
return null;
}
Set<ListNode> set = new HashSet<>();
ListNode temp_head = pHead;
while (temp_head != null) {
if (set.contains(temp_head)) {
return temp_head;
}
set.add(temp_head);
temp_head = temp_head.next;
}
return null;
}
}
原文:https://www.cnblogs.com/BaoZiY/p/11183560.html