-
Notifications
You must be signed in to change notification settings - Fork 0
/
200212-2.cpp
75 lines (71 loc) · 1.65 KB
/
200212-2.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
// https://leetcode-cn.com/problems/linked-list-cycle-ii/
// 进阶:使用常量内存(快慢指针法)
#include <cstdio>
#include <unordered_set>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if (!head || !head->next) return NULL;
ListNode* p = head;
ListNode* q = head->next;
while (p != q) {
p = (p ? p->next : NULL);
q = (q ? q->next : NULL); q = (q ? q->next : NULL);
}
if (!p) return NULL;
ListNode* tail = p;
for (p = head; p != tail; p = p->next) {
for (q = tail->next; q != tail; q = q->next) {
if (q == p) return p;
}
}
return p;
}
};
void release(ListNode* t)
{
unordered_set<ListNode*> s;
ListNode* p = t;
for (; p && s.find(p) == s.end(); p = p->next) {
s.insert(p);
}
for (auto e : s) delete e;
}
int main()
{
Solution s;
{
ListNode* n0 = new ListNode(3);
ListNode* n1 = new ListNode(2);
ListNode* n2 = new ListNode(0);
ListNode* n3 = new ListNode(-4);
n0->next = n1; n1->next = n2; n2->next = n3;
n3->next = n1; // pos = 1
ListNode* p = s.detectCycle(n0);
if (p) printf("%d\n", p->val); else printf("null\n"); // answer: 2
release(n0);
}
{
ListNode* n0 = new ListNode(1);
ListNode* n1 = new ListNode(2);
n0->next = n1;
n1->next = n0; // pos = 0
ListNode* p = s.detectCycle(n0);
if (p) printf("%d\n", p->val); else printf("null\n"); // answer: 1
release(n0);
}
{
ListNode* n0 = new ListNode(1);
; // pos = -1
ListNode* p = s.detectCycle(n0);
if (p) printf("%d\n", p->val); else printf("null\n"); // answer: null
release(n0);
}
return 0;
}