141. 环形链表

141. 环形链表 - 力扣(LeetCode)

二刷

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;
  }
};

没什么好说的。

三刷

class Solution {
public:
  bool hasCycle(ListNode *head) {
    ListNode *Slow = head, *Fast = head;
    while (Fast && Fast->next) {
      Slow = Slow->next;
      Fast = Fast->next->next;
      if (Fast == Slow)
        return true;
    }
    return false;
  }
};