Skip to content

Commit 868a401

Browse files
Merge pull request TheAlgorithms#409 from pomkarnath98/cycle-detection-in-linkedlist
Cycle detection in linkedlist
2 parents 41f4e2c + 3f755cf commit 868a401

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* A LinkedList based solution for Detect a Cycle in a list
3+
* https://en.wikipedia.org/wiki/Cycle_detection
4+
*/
5+
6+
function main () {
7+
/*
8+
Problem Statement:
9+
Given head, the head of a linked list, determine if the linked list has a cycle in it.
10+
11+
Note:
12+
* While Solving the problem in given link below, don't use main() function.
13+
* Just use only the code inside main() function.
14+
* The purpose of using main() function here is to aviod global variables.
15+
16+
Link for the Problem: https://leetcode.com/problems/linked-list-cycle/
17+
*/
18+
const head = '' // Reference to head is given in the problem. So please ignore this line
19+
let fast = head
20+
let slow = head
21+
22+
while (fast != null && fast.next != null && slow != null) {
23+
fast = fast.next.next
24+
slow = slow.next
25+
if (fast === slow) {
26+
return true
27+
}
28+
}
29+
return false
30+
}
31+
32+
main()

0 commit comments

Comments
 (0)