File tree 2 files changed +17
-0
lines changed
src/_DataStructures_/LinkedList/loop-in-list
2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
12
12
- [ Singly Linked List] ( src/_DataStructures_/LinkedList )
13
13
- [ N Element From Last] ( src/_DataStructures_/LinkedList/element-from-last )
14
14
- [ Middle Node] ( src/_DataStructures_/LinkedList/middle-node )
15
+ - [ Detect Loop] ( src/_DataStructures_/LinkedList/loop-in-list )
15
16
- [ Stack] ( src/_DataStructures_/Stack )
16
17
- [ Implement Queue Using Stack] ( src/_DataStructures_/Stack/immitate-queue-using-stack )
17
18
- [ Baseball Game] ( src/_DataStructures_/Stack/baseball-game )
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments