diff --git a/CPP/LinkedList/Linked List Cycle II.cpp b/CPP/LinkedList/Linked List Cycle II.cpp new file mode 100644 index 00000000..c54e1c5f --- /dev/null +++ b/CPP/LinkedList/Linked List Cycle II.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + ListNode *detectCycle(ListNode *head) { + ListNode* slow = head; + ListNode* fast = head; + + while(fast != NULL && fast->next != NULL){ + slow = slow->next; + fast = fast->next->next; + if(slow == fast){ + slow = head; + while(slow != fast){ + slow = slow->next; + fast = fast->next; + } + return slow; + } + } + return NULL; + } +};