/**
 * struct ListNode {
 *  int val;
 *  struct ListNode *next;
 *  ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @return bool布尔型
     */
    bool hasCycle(ListNode* head) {
        // write code here
        ListNode* slow = head;
        ListNode* fast = head->next;
        while (fast != NULL && fast->next != NULL) {
            slow = slow->next;
            fast = fast->next->next;
            if (slow->val == fast->val)
                return true;
        }
        return false;
    }
};

一、题目考察的知识点

快慢指针判断是否存在环

二、题目解答方法的文字分析

直接快慢指针判断判断一下是否有环,具体看代码

三、本题解析所用的编程语言

c++