Skip to content

Commit e931a4c

Browse files
author
10kartik
committed
Added Middle of linked-list implementation.
1 parent 644d7f7 commit e931a4c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
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://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()

0 commit comments

Comments
 (0)