/* 141. Linked List Cycle First submitted: January 16, 2022 Last submitted: September 30, 2024 Runtime: 11 ms (beats 43.81%) Memory: 10.70 MB (beats 85.26%) */ /** * 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 && fast->next != NULL) { slow = slow->next; fast = fast->next->next; if (fast == slow) return true; } return false; } };