原题链接:https://leetcode-cn.com/problems/linked-list-cycle/
快慢指针
bool
hasCycle(ListNode
*head
) {
ListNode
*p
= head
, *q
=head
;
while (q
&& q
->next
) {
p
= p
->next
;
q
= q
->next
->next
;
if (p
== q
) return true
;
}
return false
;
}
转载请注明原文地址:https://tech.qufami.com/read-12734.html