-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode141_code_linked_list_cycle.cpp
165 lines (133 loc) · 3.41 KB
/
leetcode141_code_linked_list_cycle.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @file leetcode141_linked_list_cycle.cpp
* @author wangguibao([email protected])
* @date 2023/06/03 10:42
* @brief https://leetcode.com/problems/linked-list-cycle
*
* Two solutions provided.
**/
#include <iostream>
#include <set>
#include <vector>
/**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
/*
* O(n) space solution
* When traversing the list, if a node is seen a second time, then there's
* a cycle.
*/
bool hasCycle1(ListNode *head) {
if (head == nullptr) {
return false;
}
std::set<ListNode*> nodes;
ListNode* p = head;
while (p) {
if (nodes.find(p) != nodes.end()) {
return true;
}
nodes.insert(p);
p = p->next;
}
return false;
}
/*
* O(1) space solution
* While traversing the list, if fast pointer catches up with the slow
* pointer, then there is a cycle
*/
bool hasCycle2(ListNode *head) {
if (head == nullptr) {
return false;
}
ListNode* p1 = head; // The slow pointer
ListNode* p2 = head; // The fast pointer
while (p1 && p2) {
p1 = p1->next;
if (!p1) {
break;
}
p2 = p2->next;
if (!p2) {
break;
}
if (!p2) {
break;
}
p2 = p2->next;
if (p2 == p1) {
return true;
}
}
return false;
}
};
ListNode* createListFromVec(std::vector<int> nums, int tailPos) {
ListNode* head = nullptr;
ListNode* cur = nullptr;
for (auto x: nums) {
ListNode* node = new ListNode(x);
if (head == nullptr) {
head = node;
cur = head;
} else {
cur->next = node;
cur = cur->next;
}
}
if (tailPos >= 0) {
ListNode* p = head;
for (int i = 0; i < tailPos; ++i) {
p = p->next;
}
cur->next = p;
}
return head;
}
void printList(ListNode* head, int nodeCount) {
ListNode* p = head;
int i = 1;
std::cout << "[";
while(p && (i <= nodeCount)) {
std::cout << p->val << " ";
i++;
p = p->next;
};
std::cout << "]" << std::endl;
}
int main() {
while (1) {
std::cout << "Number of elements (0 or negative exits): " << std::endl;
int n;
if (!(std::cin >> n)) {
return 0;
}
if (n <= 0) {
return 0;
}
std::vector<int> l;
std::cout << "Input " << n << " elements" << std::endl;
for (int i = 0; i < n; ++i) {
int ele;
std::cin >> ele;
l.push_back(ele);
}
std::cout << "Next pointer of tail element index: ";
int pos;
std::cin >> pos;
ListNode* h = createListFromVec(l, pos);
printList(h, n);
Solution solution;
auto result = solution.hasCycle2(h);
std::cout << (result? "true" : "false") << std::endl;
}
return 0;
}