解题思路是有两个指针,一个快指针,一个慢指针。慢指针每次移动一个结构体,快指针每次移动两个结构体。如果他们指向空,则没有环,如果他们相等,则有环。 C语言代码如下

 
 * @param head ListNode类 
 * @return bool布尔型
 */
bool hasCycle(struct ListNode* head ) {
    // write code here
    struct ListNode*fast=head;
    struct ListNode*slow=head;
    if(slow==NULL||slow->next==NULL||fast->next->next==NULL){
        return false;
    }//首先判断是否至少有三个结构体
    fast=fast->next->next;
    slow=slow->next;
    while(fast!=NULL){
        if(slow->next==NULL||fast->next->next==NULL||fast->next==NULL){
        return false;//判断下次循环是否能够循环
    }
        if(fast!=slow){
            fast=fast->next->next;
            slow=slow->next;//不相等就循环
        }else{
            return true;
        }
    }
    return false;
}