Leetcode#141 Linked List Cycle
经典链表题目,快慢指针法。曾经是面试热门题目之一,现在有些过时了。
代码:
1 bool hasCycle(ListNode *head) { 2 ListNode *fast = head; 3 ListNode *slow = head; 4 bool running = false; 5 6 while (fast && fast->next) { 7 if (fast == slow && running) 8 return true; 9 running = true; 10 fast = fast->next->next; 11 slow = slow->next; 12 } 13 14 return false; 15 }