1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution 10 { 11 public: 12 bool hasCycle(ListNode *head) 13 { 14 ListNode* fast = head; 15 ListNode* slow = head; 16 while(fast && fast->next && slow) 17 { 18 fast = fast->next->next; 19 slow = slow->next; 20 if(fast == slow) return true; 21 } 22 return false; 23 } 24 };
原文:https://www.cnblogs.com/yuhong1103/p/12621466.html