class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *slow = head, *fast = head;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
            if (slow == fast) break;
        }
        if (!fast || !fast->next) return NULL;
        slow = head;
        while (slow != fast) {
            slow = slow->next;
            fast = fast->next;
        }
        return fast;
    }
};

相关文章:

  • 2021-07-03
  • 2021-09-12
  • 2021-06-04
  • 2021-08-25
  • 2021-12-23
  • 2021-08-11
  • 2021-11-01
猜你喜欢
  • 2021-12-10
  • 2022-01-19
相关资源
相似解决方案