From 70aba563002daf4a89127d87bae884a87fdc5574 Mon Sep 17 00:00:00 2001 From: Chhatrapal Nayak Date: Tue, 22 Oct 2019 15:46:42 +0530 Subject: [PATCH] Create cycle.cpp --- LeetCode/cycle1.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 LeetCode/cycle1.cpp diff --git a/LeetCode/cycle1.cpp b/LeetCode/cycle1.cpp new file mode 100644 index 00000000..a6d81f66 --- /dev/null +++ b/LeetCode/cycle1.cpp @@ -0,0 +1,27 @@ +// Complete the has_cycle function below. + +/* + * For your reference: + * + * SinglyLinkedListNode { + * int data; + * SinglyLinkedListNode* next; + * }; + * + */ +bool has_cycle(SinglyLinkedListNode* head) { + if(head==nullptr) + return false; + SinglyLinkedListNode *slow=head,*fast=head; + + while(fast && fast->next) + { + slow = slow->next; + fast = fast->next->next; + if(slow == fast) + return true; + } + return false; + + +}