File tree Expand file tree Collapse file tree
Data-Structures/Linked-List Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ( )
You can’t perform that action at this time.
0 commit comments