//LeetCode141.环形链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode* head) {
ListNode* p = head;
ListNode* q = head;
while (q != NULL) {
p = p->next;
q = q->next;
if (q == NULL) return false;
q = q->next;
if (p == q) return true;
}
return false;
}
};
LeetCode141.环形链表
最新推荐文章于 2024-08-21 22:23:47 发布