-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetectCycle.cpp
29 lines (27 loc) · 935 Bytes
/
detectCycle.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
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(nullptr){}
};
ListNode *detectCycle(ListNode *head) {
if(head==nullptr || head->next==nullptr) return nullptr;
ListNode *slow = head;
ListNode *fast = head;
//try to find the point they meet
while(fast!=nullptr&&fast->next!=nullptr) {
fast=fast->next->next;
slow=slow->next;
if(slow==fast) break;
}
// if there's no cycle, the above while loop would exit with nullptr ending
if(fast==nullptr||fast->next==nullptr) return nullptr;
// one pointer starts from where they met
// the other point starts from the head
// they continue with the same speed, and they would meet at the starting point of the cycle.
slow = head;
while(slow!=fast){
slow=slow->next;
fast=fast->next;
}
return slow;
}