-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
87 lines (67 loc) · 2.33 KB
/
Source.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <unordered_set>
#include "ListNode.h"
using namespace std;
bool hasCycle(ListNode* head);
ListNode* buildCyclicalList(const std::vector<int>& values, int cycleIndex);
void deleteList(ListNode* head);
int main() {
cout << std::boolalpha; // set the boolalpha flag for cout
// Test Case 1
cout << "Input: head = [3, 2, 0, -4], pos = 1\nExpected Output: true\nActual Output: "
<< hasCycle(buildCyclicalList({ 3, 2, 0, -4}, 1)) << "\n\n";
// Test Case 2
std::cout << "Input: head = [1, 2], pos = 0\nExpected Output: true\nActual Output: "
<< hasCycle(buildCyclicalList({ 1, 2 }, 0)) << "\n\n";
// Test Case 3
std::cout << "Input: head = [1], pos = -1\nExpected Output: false\nActual Output: "
<< hasCycle(buildCyclicalList({ 1 }, -1)) << "\n\n";
// Test Case 4
std::cout << "Input: head = [], pos = -1\nExpected Output: false\nActual Output: "
<< hasCycle({}) << "\n\n";
// Test Case 5
std::cout << "Input: head = [0], pos = 1\nExpected Output: false\nActual Output: "
<< hasCycle({}) << "\n\n";
return 0;
}
bool hasCycle(ListNode* head) {
// Edge Cases
if (head == nullptr || head->next == nullptr) { return false; }
// Containers
unordered_set<ListNode*> nodeList;
while (head != nullptr) {
// The insert method returns a pair, with the second element of the pair indicating whether the insertion was successful
bool result = (nodeList.insert(head)).second;
// If false, the address is not unique, and the linked list contains a cycle
if (!result) {
return true;
}
// Get the next node
head = head->next;
}
return false;
}
// To test our algorithm, we will construct a method that generates a cyclical linked list at the specified index
ListNode* buildCyclicalList(const std::vector<int>& values, int cycleIndex) {
if (values.empty()) { return nullptr; }
ListNode dummy(0);
ListNode* current = &dummy;
std::vector<ListNode*> nodes;
for (int element : values) {
current->next = new ListNode(element);
current = current->next;
nodes.push_back(current);
}
// Create a cycle by linking the tail to the node specified by the cycleIndex
if (cycleIndex >= 0 && cycleIndex < values.size()) {
current->next = nodes[cycleIndex];
}
return dummy.next;
}
void deleteList(ListNode* head) {
while (head != nullptr) {
ListNode* temp = head;
head = head->next;
delete temp;
}
}