环形链表
class Solution {
public:
bool hasCycle(ListNode
*head
) {
bool flag
= false;
ListNode
*fast
= head
, *slow
= head
;
while(fast
&& slow
){
if(fast
== slow
&& flag
) return true;
slow
= slow
->next
;
if(!fast
->next
) return false;
else fast
= fast
->next
->next
;
flag
= true;
}
return false;
}
};
转载请注明原文地址:https://blackberry.8miu.com/read-46560.html