Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
711 B
2
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;
    }
};
Leave a Comment