From 711d70e310bb685551cc6a25d8ea46b037f8c695 Mon Sep 17 00:00:00 2001 From: Keisuke KUDO <151166401+Apo-Matchbox@users.noreply.github.com> Date: Thu, 5 Mar 2026 19:16:40 -0500 Subject: [PATCH] 142.shadowing(Floyd) --- 142/142.shadowing(Floyd).md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 142/142.shadowing(Floyd).md diff --git a/142/142.shadowing(Floyd).md b/142/142.shadowing(Floyd).md new file mode 100644 index 0000000..3ef7037 --- /dev/null +++ b/142/142.shadowing(Floyd).md @@ -0,0 +1,27 @@ +# Floyd Solution + +```cpp +class Solution { + public: + ListNode *detectCycle(ListNode *head) { + ListNode* slow = head; + ListNode* fast = head; + + while (fast && fast->next) { + slow = slow->next; + fast = fast->next->next; + + if (slow == fast) { + slow = fast; + + while (slow != fast) { + slow = slow->next; + fast = slow->next; + } + return slow; + } + } + return nullptr; + } +}; +```