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 11/**
2- * A LinkedList based solution for Detect a Cycle in a list
2+ * A LinkedList based solution for finding middle node of linked list.
33 * https://afteracademy.com/blog/middle-of-the-linked-list
44 */
5-
6- function main ( ) {
5+ class MiddleOfLL {
6+ solution ( head ) {
77 /*
88 Problem Statement:
99 Given the head of a singly linked list, return the middle node of the linked list.
1010 If there are two middle nodes, return the second middle node.
1111
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-
1712 Link for the Problem: https://leetcode.com/problems/middle-of-the-linked-list/
1813 */
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
2216
23- if ( head . next == null ) { return head }
17+ if ( head . next == null ) { return head }
2418
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
2824 }
29- return slow
3025}
3126
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