题解 | #牛牛队列成环#
牛牛队列成环
https://www.nowcoder.com/practice/38467f349b3a4db595f58d43fe64fcc7
/**
* 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++
牛客课后习题题解 文章被收录于专栏
等待蜕变

查看9道真题和解析