Skip to content

Commit 5e3e4a8

Browse files
committed
update: detect loop in Linked List
1 parent aaeffd2 commit 5e3e4a8

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
1212
- [Singly Linked List](src/_DataStructures_/LinkedList)
1313
- [N Element From Last](src/_DataStructures_/LinkedList/element-from-last)
1414
- [Middle Node](src/_DataStructures_/LinkedList/middle-node)
15+
- [Detect Loop](src/_DataStructures_/LinkedList/loop-in-list)
1516
- [Stack](src/_DataStructures_/Stack)
1617
- [Implement Queue Using Stack](src/_DataStructures_/Stack/immitate-queue-using-stack)
1718
- [Baseball Game](src/_DataStructures_/Stack/baseball-game)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Floyd’s Cycle-Finding Algorithm
2+
3+
function detechLoop(linkedList) {
4+
let slow = linkedList.getFirst();
5+
let fast = linkedList.getFirst();
6+
7+
while (fast.next && fast.next.next) {
8+
slow = slow.next;
9+
fast = fast.next.next;
10+
11+
if (slow === fast) {
12+
return true;
13+
}
14+
}
15+
return false;
16+
}

0 commit comments

Comments
 (0)