用java解决还是很好的,创建一个ArrayList,若新的结点不在List中,则加入List,在其中的话,返回该结点 就是入口了
public class Solution {
public ListNode detectCycle(ListNode head) {
ArrayList<ListNode> arr = new ArrayList<>();
ListNode l = head;
while(l!=null){
if(arr.contains(l)){
return l;
}else{
arr.add(l);
}
l = l.next;
}
return null;
}
京公网安备 11010502036488号