public ListNode detectCycle(ListNode head) {
if (head == null){
return null;
}
ListNode pos = head;
Set<ListNode> set =new HashSet<ListNode>();
set.add(pos);
pos = pos.next;
while (pos != null &&!set.contains(pos)) {
if (!set.add(pos)){
return pos;
}
pos = pos.next;
}
return null;
}
求问,我这代码有什么逻辑错误吗,为什么样例过不了呢?
而且,我应该和官方的代码的思路一样啊
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode pos = head;
Set<ListNode> visited = new HashSet<ListNode>();
while (pos != null) {
if (visited.contains(pos)) {
return pos;
} else {
visited.add(pos);
}
pos = pos.next;
}
return null;
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/linked-list-cycle-ii/solutions/441131/huan-xing-lian-biao-ii-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
有没有,大佬能帮我看看 , 这是哪里的问题啊?