解题图解:
文章来源:https://uudwc.com/A/gV3yg
代码如下: 文章来源地址https://uudwc.com/A/gV3yg
bool hasCycle(struct ListNode *head) {
struct ListNode * fast=head;
//在这里fast是快指针
//head作为low指针
//因为这个题不需要做修改也只需返回true或false
//就少开辟一个空间
while(fast!=NULL&&fast->next!=NULL){
head=head->next;
fast=fast->next->next;
//fast每次前进两个节点
//head每次前进一个
if(head==fast){
return true;
}
}
return false;
}