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