File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Data-Structures/Linked-List Expand file tree Collapse file tree 1 file changed +32
-0
lines changed 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://afteracademy.com/blog/middle-of-the-linked-list
4
+ */
5
+
6
+ function main ( ) {
7
+ /*
8
+ Problem Statement:
9
+ Given the head of a singly linked list, return the middle node of the linked list.
10
+ If there are two middle nodes, return the second middle node.
11
+
12
+ Note:
13
+ * While Solving the problem in given link below, don't use main() function.
14
+ * Just use only the code inside main() function.
15
+ * The purpose of using main() function here is to avoid global variables.
16
+
17
+ Link for the Problem: https://leetcode.com/problems/middle-of-the-linked-list/
18
+ */
19
+ const head = '' // Reference to head is given in the problem. So please ignore this line
20
+ let fast = head
21
+ let slow = head
22
+
23
+ if ( head . next == null ) { return head }
24
+
25
+ while ( fast != null && fast . next != null ) {
26
+ fast = fast . next . next
27
+ slow = slow . next
28
+ }
29
+ return slow
30
+ }
31
+
32
+ main ( )
You can’t perform that action at this time.
0 commit comments