-
Notifications
You must be signed in to change notification settings - Fork 0
/
142.cc
52 lines (50 loc) · 1.33 KB
/
142.cc
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
#include<cstdio>
#include<cstddef>
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head==NULL||head->next==NULL){
return NULL;
}
ListNode *slow=head;
ListNode *fast=head->next;
while(slow!=fast){
if(slow->next==NULL||fast->next==NULL||fast->next->next==NULL){
return NULL;
}
slow=slow->next;
fast=fast->next->next;
printf("fast:%d\n",fast->val);
printf("slow:%d\n",slow->val);
}
slow=slow->next;
ListNode *p=head;
while(p!=slow){
p=p->next;
slow=slow->next;
printf("p:%d\n",p->val);
printf("slow:%d\n",slow->val);
}
return p;
}
};
int main(){
ListNode *head=new ListNode(3);
head->next=new ListNode(2);
// ListNode *p=head->next;
head->next->next=new ListNode(0);
head->next->next->next=new ListNode(-4);
head->next->next->next->next=head->next;
Solution s;
printf("%d\n",s.detectCycle(head)->val);
ListNode *head1=new ListNode(1);
head1->next=new ListNode(2);
printf("%d\n",s.detectCycle(head1));
return 0;
}