From 2a260f6543e2b2a7f8ec5eaee1fcb4eea928274a Mon Sep 17 00:00:00 2001 From: Ashutosh <106524453+Ashutosh741@users.noreply.github.com> Date: Thu, 13 Oct 2022 18:12:27 +0530 Subject: [PATCH] Create Linked List Cycle II.cpp --- CPP/LinkedList/Linked List Cycle II.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 CPP/LinkedList/Linked List Cycle II.cpp 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; + } +};