-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinkedListCycle_two_pointers.cpp
More file actions
31 lines (25 loc) · 1.09 KB
/
linkedListCycle_two_pointers.cpp
File metadata and controls
31 lines (25 loc) · 1.09 KB
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == NULL) return false; //if head is null, there will never be a loop
ListNode *sp = head; //slow pointer: moves one step at a time
ListNode *fp = head->next; //fast pointer: moves two steps at a time
while(fp != NULL && fp->next != NULL){ //check if fast pointer reaches the end of the
// linked list.
if(fp == sp) //if slow pointer is equal to fast pointer, that is, they are
return true; // on the same node, we got our loop.
fp = fp->next->next; //Fast pointer moves 2 steps ahead
sp = sp->next; //Slow pointer moves 1 step ahead
}
//If end of linked list is reached, there is no loop, and we
return false; // return false.
}
};