-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListCycleII.h
44 lines (36 loc) · 1001 Bytes
/
LinkedListCycleII.h
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
/*
Author: naiyong, aonaiyong@gmail.com
Date: Sep 22, 2014
Problem: Linked List Cycle II
Difficulty: 3
Source: https://oj.leetcode.com/problems/linked-list-cycle-ii/
Notes:
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Follow up:
Can you solve it without using extra space?
Solution: Two pointers.
*/
#ifndef LINKEDLISTCYCLEII_H_
#define LINKEDLISTCYCLEII_H_
#include "ListNode.h"
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
break;
}
if (!fast || !fast->next)
return nullptr;
fast = head;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
};
#endif /* LINKEDLISTCYCLEII_H_ */