class Solution { public: /** * @param head: The first node of linked list. * @return: True if it has a cycle, or false */ bool hasCycle(ListNode * head) { // write your code here if (!head || !head->next) return false; ListNode *fast = head->next->next, *slow = head; while (fast && fast->next) { if (fast == slow && fast != NULL) return true; fast = fast->next->next; slow = slow->next; } return false; } };
-------------end of file
thanks for reading-------------