题目传送: ​​https://leetcode.cn/problems/linked-list-cycle-ii/​

运行效率:

Leetcode142. 环形链表 II_数据结构


代码如下:

public ListNode detectCycle(ListNode head) {
// 处理边界情况
if (head == null || head.next == null) {
return null;
}
HashSet<ListNode> set = new HashSet<>();
ListNode cur=head;
while(cur != null) {
if(set.contains(cur)) {
return cur;
}
set.add(cur);
cur = cur.next;
}
return null;
}