0141.cpp (357B)
1 class Solution { 2 public: 3 bool hasCycle(ListNode *head) { 4 if (!head) return false; 5 6 ListNode *slow, *fast; 7 fast = slow = head; 8 while (fast->next && fast->next->next) { 9 fast = fast->next->next; 10 slow = slow->next; 11 if (fast == slow) return true; 12 } 13 return false; 14 } 15 };