题解 | 牛牛队列成环
牛牛队列成环
https://www.nowcoder.com/practice/38467f349b3a4db595f58d43fe64fcc7
- 非常奇怪,其实只要比较 f == s 即可。但是牛客网不可以。
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}
ListNode f = head, s = head;
while (f != null && f.next != null) {
f = f.next.next;
s = s.next;
if (f != null && s != null && f.val == s.val) {
return true;
}
}
return false;
}
}
查看29道真题和解析
