#include <stdbool.h>
#include <stdlib.h>
/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

/**
 * @author Senky
 * @date 2023.04.15
 * @par url https://www.nowcoder.com/creation/manager/content/584337070?type=column&status=-1
 * @brief 快慢指针,slow一次移动一个结点,fast一次移动两个结点,只要有环,fast指针迟早会追上slow
 * @param head ListNode类 
 * @return bool布尔型
 */
bool hasCycle(struct ListNode* head ) {
    // write code here
    struct ListNode* slow = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* fast = (struct ListNode*)malloc(sizeof(struct ListNode));

    slow = head;
    fast = head;

    int IsCycle = 0;
    /*
     *fast是判断链表是否为空,fast->nest是判断链表是否有表尾
     *因为快指针一次移动两个结点fast = (fast->next)->next,所以(fast->next)不能为空
    */
    while(fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;

        if(slow == fast)
        {
            IsCycle = 1;
            break;
        }
    }

    return IsCycle;    
}