题目
菜鸟解答
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
struct ListNode *node_slow, *node_quick;
if (NULL == head || NULL == head->next)
return false;
node_slow = node_quick = head;
while (node_quick != NULL) {
node_quick = node_quick->next;
if (NULL == node_quick)
return false;
node_quick = node_quick->next;
if (NULL == node_quick)
return false;
node_slow = node_slow->next;
if (node_slow == node_quick)
return true;
}
return false;
}
官方解答的该算法的复杂度分析
原链接:https://leetcode-cn.com/problems/linked-list-cycle/solution/huan-xing-lian-biao-by-leetcode/