-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path环形链表 (hasCycle).cpp
65 lines (56 loc) · 1.09 KB
/
环形链表 (hasCycle).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
/*
**没什么难度,利用了set的不存在重复元素的性质
*/
#include <string>
#include <sstream>
#include <algorithm>
#include <stack>
#include <cmath>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
bool hasCycle(ListNode *head) {
set<ListNode *> a;
ListNode *p=head;
while (p != NULL) {
int v1 = a.size();
a.insert(p);
int v2 = a.size();
if (v1 == v2) {
return true;
}
p = p->next;
}
return false;
}
int main() {
ListNode * A1 = new ListNode(1);
ListNode * A2 = new ListNode(2);
ListNode * A3 = new ListNode(3);
ListNode * B1 = new ListNode(1);
ListNode * B2 = new ListNode(3);
ListNode * B3 = new ListNode(4);
ListNode * C1 = new ListNode(2);
ListNode * C2 = new ListNode(6);
A1->next = A2;
A2->next = A3;
B1->next = B2;
B2->next = B3;
C1->next = C2;
vector<ListNode*> lists;
lists.push_back(A1);
lists.push_back(B1);
lists.push_back(C1);
ListNode * AAA;
int *p = new int(1);
int a;
cout << hasCycle(A1);
system("pause");
return 0;
}