Skip to content

Latest commit

 

History

History
19 lines (18 loc) · 424 Bytes

File metadata and controls

19 lines (18 loc) · 424 Bytes
  • 使用两个指针,快指针一次移动两步,慢指针一次移动一步,若链表有环,则二者必将相遇
class Solution {
 public:
  bool hasCycle(ListNode* head) {
    ListNode* slow = head;
    ListNode* fast = head;
    while (fast && fast->next) {
      slow = slow->next;
      fast = fast->next->next;
      if (slow == fast) {
        return true;
      }
    }
    return false;
  }
};