-
Notifications
You must be signed in to change notification settings - Fork 14
/
Checking_cycle_alternative.cpp
58 lines (50 loc) · 1.67 KB
/
Checking_cycle_alternative.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
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
#include <cassert>
#include <iostream>
#include <memory>
#include "./Linked_list_prototype.h"
using std::cout;
using std::endl;
using std::make_shared;
// @include
shared_ptr<ListNode<int>> has_cycle(const shared_ptr<ListNode<int>>& head) {
shared_ptr<ListNode<int>> fast = head, slow = head;
while (slow && slow->next && fast && fast->next && fast->next->next) {
slow = slow->next, fast = fast->next->next;
if (slow == fast) { // there is a cycle.
// Tries to find the start of the cycle.
slow = head;
// Both pointers advance at the same time.
while (slow != fast) {
slow = slow->next, fast = fast->next;
}
return slow; // slow is the start of cycle.
}
}
return nullptr; // means no cycle.
}
// @exclude
int main(int argc, char* argv[]) {
shared_ptr<ListNode<int>> L3 =
make_shared<ListNode<int>>(ListNode<int>{3, nullptr});
shared_ptr<ListNode<int>> L2 =
make_shared<ListNode<int>>(ListNode<int>{2, L3});
shared_ptr<ListNode<int>> L1 =
make_shared<ListNode<int>>(ListNode<int>{1, L2});
// should output "L1 does not have cycle."
assert(has_cycle(L1) == nullptr);
cout << "L1 " << (has_cycle(L1) ? "has" : "does not have") << " cycle."
<< endl;
// make it a cycle
L3->next = L2;
// should output "L1 has cycle, located at node has value 2"
assert(has_cycle(L1) != nullptr);
assert(has_cycle(L1)->data == 2);
auto temp = has_cycle(L1);
if (temp) {
cout << "L1 has cycle, located at node has value " << temp->data << endl;
} else {
cout << "L1 does not have cycle" << endl;
}
return 0;
}