-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (110 loc) · 2.63 KB
/
main.cpp
File metadata and controls
123 lines (110 loc) · 2.63 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <bits/stdc++.h>
using namespace std;
/**
* Definition for singly-linked list.
*/
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* create_list(const vector<int>& array, int pos)
{
if (array.size() == 0)
return NULL;
ListNode* head = new ListNode(array[0]);
ListNode* current = head;
ListNode* cycling_node = NULL;
for (int i = 1; i < (int)array.size(); ++i)
{
current->next = new ListNode(array[i]);
current = current->next;
if (i == pos)
cycling_node = current;
}
if (pos == 0)
current->next = head;
else
current->next = cycling_node;
return head;
}
void print_list(ListNode* head)
{
ListNode* current = head;
set<ListNode*> visited;
while (current != NULL)
{
if (visited.find(current) != visited.end())
break;
visited.insert(current);
cout << current->val << endl;
current = current->next;
}
if (current) // if cycle exists
cout << "next in cycle: " << current->val << endl;
}
void delete_list(ListNode* head, set<ListNode*>* handled = NULL)
{
bool created = false;
if (!handled)
{
handled = new set<ListNode*>;
created = true;
}
handled->insert(head);
if (handled->find(head->next) == handled->end() && head->next)
delete_list(head->next, handled);
delete head;
if (created)
delete handled;
}
class Solution
{
public:
/**
* Check if the list has a cycle using set<ListNode*>
*/
bool has_cycle_2(ListNode* head)
{
ListNode* current = head;
set<ListNode*> visited;
while (current != NULL)
{
if (visited.find(current) != visited.end())
return true;
visited.insert(current);
current = current->next;
}
return false;
}
/**
* Check if the list has a cycle using fast and slow pointers
*/
bool hasCycle(ListNode* head)
{
ListNode* fast = head;
ListNode* slow = head;
while (fast != NULL)
{
slow = slow->next;
fast = fast->next;
if (fast == NULL)
return false;
fast = fast->next;
if (slow == fast)
return true;
}
return false;
}
};
int main()
{
vector<int> input = {3,2,0,-4};
int pos = 1;
ListNode* head = create_list(input, pos);
print_list(head);
cout << "Does the list contain a cycle? " << Solution().hasCycle(head) << endl;
delete_list(head);
return 0;
}