forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedListCycle.cpp
44 lines (33 loc) · 1019 Bytes
/
linkedListCycle.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
// Source : https://oj.leetcode.com/problems/linked-list-cycle/
// Author : Hao Chen
// Date : 2014-07-03
/**********************************************************************************
*
* Given a linked list, determine if it has a cycle in it.
*
* Follow up:
* Can you solve it without using extra space?
*
*
**********************************************************************************/
/*
* if there is a cycle in the list, then we can use two pointers travers the list.
*
* one pointer traverse one step each time, another one traverse two steps each time.
*
* so, those two pointers meet together, that means there must be a cycle inside the list.
*/
bool hasCycle(ListNode *head) {
ListNode* p1;
ListNode* p2;
if (head==NULL) return false;
p1=head;
p2=head;
while (p1!=NULL && p2!=NULL){
p1=p1->next;
if (p2->next == NULL) return false;
p2=p2->next->next;
if (p1==p2) return true;
}
return false;
}