
这道题和上一道题几乎没有任何区别啊,为什么还是中等难度,我用上一道题的解法一分钟就写出来了,只不过返回的不是true和false而是节点,以下是我的代码:
- public class Solution {
- public ListNode detectCycle(ListNode head) {
- ListNode h = head;
- Set
set = new HashSet<>(); - while(h!=null){
- if(!set.add(h)){
- return h;
- }else{
- h = h.next;
- }
- }
- return null;
- }
- }
看看题解有什么不一样的,没有任何不一样,和LeetCode141.环形链表_荔枝味啊~的博客-CSDN博客
上一题一模一样,第一种解法用HashSet,第二种解法用快慢指针,可以看我的上一篇博客。