题目要求
解题思路
快慢指针法
C++代码
/**
* 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* slow = head;
ListNode* fast = head;
while (fast != NULL) {
if (fast != NULL && fast->next!= NULL) {
fast = fast->next->next;
slow = slow->next;
} else {
return 0;
}
if (slow == fast) {
return 1;
}
}
return 0;
}
};
执行结果