/**
* 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 *one, *two;
if (head == NULL || head->next == NULL)
return false;
one = head; two = head;
while (two && two->next && two->next->next)
{
one = one->next;
two = two->next->next;
if (one == two)
{
return true;
}
}
return false;
}
};
【LeetCode】Linked List Cycle
最新推荐文章于 2021-11-01 14:38:45 发布