Untitled
unknown
plain_text
17 days ago
711 B
2
Indexable
Never
/** * 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; } };
Leave a Comment