File tree Expand file tree Collapse file tree 2 files changed +53
-17
lines changed
Data-Structures/Linked-List Expand file tree Collapse file tree 2 files changed +53
-17
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
- * A LinkedList based solution for Detect a Cycle in a list
2
+ * A LinkedList based solution for finding middle node of linked list.
3
3
* https://afteracademy.com/blog/middle-of-the-linked-list
4
4
*/
5
-
6
- function main ( ) {
5
+ class MiddleOfLL {
6
+ solution ( head ) {
7
7
/*
8
8
Problem Statement:
9
9
Given the head of a singly linked list, return the middle node of the linked list.
10
10
If there are two middle nodes, return the second middle node.
11
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
12
Link for the Problem: https://leetcode.com/problems/middle-of-the-linked-list/
18
13
*/
19
- const head = '' // Reference to head is given in the problem. So please ignore this line
20
- let fast = head
21
- let slow = head
14
+ let fast = head
15
+ let slow = head
22
16
23
- if ( head . next == null ) { return head }
17
+ if ( head . next == null ) { return head }
24
18
25
- while ( fast != null && fast . next != null ) {
26
- fast = fast . next . next
27
- slow = slow . next
19
+ while ( fast != null && fast . next != null ) {
20
+ fast = fast . next . next
21
+ slow = slow . next
22
+ }
23
+ return slow
28
24
}
29
- return slow
30
25
}
31
26
32
- main ( )
27
+ export { MiddleOfLL }
Original file line number Diff line number Diff line change
1
+ import { MiddleOfLL } from '../MiddleOfLinkedList'
2
+ import { LinkedList } from '../SinglyLinkedList'
3
+
4
+ describe ( 'MiddleOfLinkedList' , ( ) => {
5
+ it ( 'middle node Of linked list - even length ' , ( ) => {
6
+ const list = new LinkedList ( )
7
+ list . addFirst ( 1 )
8
+ list . addLast ( 2 )
9
+ list . addLast ( 3 )
10
+ list . addLast ( 4 )
11
+ list . addLast ( 5 )
12
+ list . addLast ( 6 )
13
+ list . addLast ( 7 )
14
+
15
+ const MiddleNodeOfLinkedList = new MiddleOfLL ( ) . solution ( list . headNode )
16
+ expect ( MiddleNodeOfLinkedList . data ) . toEqual ( 4 )
17
+ } )
18
+
19
+ it ( 'middle node of linked list - odd length ' , ( ) => {
20
+ const list = new LinkedList ( )
21
+ list . addFirst ( 10 )
22
+ list . addLast ( 20 )
23
+ list . addLast ( 30 )
24
+ list . addLast ( 40 )
25
+ list . addLast ( 50 )
26
+ list . addLast ( 60 )
27
+ list . addLast ( 70 )
28
+ list . addLast ( 80 )
29
+
30
+ const MiddleNodeOfLinkedList = new MiddleOfLL ( ) . solution ( list . headNode )
31
+ expect ( MiddleNodeOfLinkedList . data ) . toEqual ( 50 )
32
+ } )
33
+
34
+ it ( 'middle node of linked list - length 1 ' , ( ) => {
35
+ const list = new LinkedList ( )
36
+ list . addFirst ( 100 )
37
+
38
+ const MiddleNodeOfLinkedList = new MiddleOfLL ( ) . solution ( list . headNode )
39
+ expect ( MiddleNodeOfLinkedList . data ) . toEqual ( 100 )
40
+ } )
41
+ } )
You can’t perform that action at this time.
0 commit comments