blob: 4aa33c62f6a79a5d0d478d10ab664c84cb408d70 (
plain) (
blame)
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
|
#include"../Seminar04/linked-list-utils.h"
bool hasCycle(LinkedListNode<int>* head) {
LinkedListNode<int> *fast = head;
LinkedListNode<int> *slow = head;
while (fast && fast->next && slow) {
fast = (fast->next)->next;
slow = slow->next;
if (fast == slow)
return true;
}
return false;
}
int main() {
using Node = LinkedListNode<int>;
Node* last = new Node(6);
Node* list = new Node(1,
new Node(2,
new Node(3,
new Node(4,
new Node(5,
last)))));
last->next = list->next->next;
hasCycle(list);
}
|