-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathhackotberfest.cpp
70 lines (60 loc) · 1.41 KB
/
hackotberfest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <unordered_set>
using namespace std;
// A Linked List Node
struct Node
{
int data;
Node* next;
};
// function to create linked list
void push(Node*& head, int data)
{
// create a new linked list node from the heap
Node* newNode = new Node;
newNode->data = data;
newNode->next = head;
head = newNode;
}
// Floyd’s cycle detection algorithm
bool detectCycle(Node* head)
{
// take two pointers – `slow` and `fast`
Node *slow = head, *fast = head;
while (fast && fast->next)
{
// move slow by one pointer
slow = slow->next;
// move fast by two pointers
fast = fast->next->next;
// if they meet any node, the linked list contains a cycle
if (slow == fast) {
return true;
}
}
// we reach here if the slow and fast pointer does not meet
return false;
}
int main()
{
// input keys
int keys[] = { 1, 2, 3, 4, 5 };
int n = sizeof(keys) / sizeof(keys[0]);
Node* head =NULL;
for (int i = n - 1; i >= 0; i--) {
push(head, keys[i]);
}
// insert cycle
head->next->next->next->next->next = head->next->next;
if (detectCycle(head)) {
cout << "Cycle Found";
}
else {
cout << "No Cycle Found";
}
return 0;
}
/*
Output::
Cycle Found
*/