class Solution { public: bool hasCycle(ListNode *head) { if(head == NULL) return false; if(head->next == NULL) return false; ListNode* p1 = head; ListNode* p2 = head; p1 = p1->next; p2 = p2->next->next; while(p2 != NULL && p2->next != NULL && p2->next->next != NULL && p1 != p2){ p1 = p1->next; p2 = p2->next->next; } if(p1 == p2) return true; else return false; } };
时间: 2024-10-19 12:07:53