-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_List_Cycle.cpp
More file actions
52 lines (43 loc) · 980 Bytes
/
Linked_List_Cycle.cpp
File metadata and controls
52 lines (43 loc) · 980 Bytes
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
// Source : https://oj.leetcode.com/problems/linked-list-cycle/
// Author : zheng yi xiong
// Date : 2014-11-28
/**********************************************************************************
*
* Given a linked list, determine if it has a cycle in it.
* Follow up:
* Can you solve it without using extra space?
*
**********************************************************************************/
#include "stdafx.h"
#include <vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class CLinked_List_Cycle {
public:
bool hasCycle(ListNode *head) {
if (NULL == head)
{
return false;
}
ListNode *pNode1 = head;
ListNode *pNode2 = head;
do
{
pNode1 = pNode1->next;
if (NULL == pNode2->next)
{
return false;
}
pNode2 = pNode2->next->next;
if (pNode1 == pNode2)
{
return true;
}
} while (NULL != pNode2);
return false;
}
};