leetcode-142. 环形链表 II

原始思路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Solution {
Set<ListNode> set = new HashSet<>();
public ListNode detectCycle(ListNode head) {
if (head == null) {
return null;
}
if (set.contains(head)){
return head;
}
set.add(head);
if (head.next!=null){
return detectCycle(head.next);
}
return null;
}
}