forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycleDetection.js
More file actions
Latest commit
24 lines (21 loc) · 578 Bytes
/
Copy pathCycleDetection.js
File metadata and controls
24 lines (21 loc) · 578 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
/**
* A LinkedList based solution for Detecting a Cycle in a list.
* https://en.wikipedia.org/wiki/Cycle_detection
*/
functiondetectCycle(head){
/*
Problem Statement:
Given head, the head of a linked list, determine if the linked list has a cycle in it.
Link for the Problem: https://leetcode.com/problems/linked-list-cycle/
*/
if(!head){returnfalse}
letslow=head
letfast=head.next
while(fast&&fast.next){
if(fast===slow){returntrue}
fast=fast.next.next
slow=slow.next
}
returnfalse
}
export{detectCycle}