Untitled
unknown
plain_text
a year ago
711 B
12
Indexable
/**
* 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) {
cin.tie(nullptr)->sync_with_stdio(0);
if (head == nullptr) {
return false;
}
ListNode* PtrLift = head;
ListNode* ptrRight = head;
while (ptrRight != nullptr && ptrRight->next != nullptr) {
PtrLift = PtrLift->next;
ptrRight = ptrRight->next->next;
if (ptrRight == PtrLift) {
return true;
}
}
return false;
}
};Editor is loading...
Leave a Comment