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