From e931a4cbcee4b25e25e00b16f992155649f5f7e1 Mon Sep 17 00:00:00 2001 From: 10kartik Date: Thu, 8 Sep 2022 20:28:03 +0530 Subject: [PATCH 1/5] Added Middle of linked-list implementation. --- .../Linked-List/MiddleOfLinkedList.js | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Data-Structures/Linked-List/MiddleOfLinkedList.js diff --git a/Data-Structures/Linked-List/MiddleOfLinkedList.js b/Data-Structures/Linked-List/MiddleOfLinkedList.js new file mode 100644 index 0000000000..b6eaa62865 --- /dev/null +++ b/Data-Structures/Linked-List/MiddleOfLinkedList.js @@ -0,0 +1,32 @@ +/** + * A LinkedList based solution for Detect a Cycle in a list + * https://afteracademy.com/blog/middle-of-the-linked-list + */ + +function main () { + /* + Problem Statement: + Given the head of a singly linked list, return the middle node of the linked list. + If there are two middle nodes, return the second middle node. + + Note: + * While Solving the problem in given link below, don't use main() function. + * Just use only the code inside main() function. + * The purpose of using main() function here is to avoid global variables. + + Link for the Problem: https://leetcode.com/problems/middle-of-the-linked-list/ + */ + const head = '' // Reference to head is given in the problem. So please ignore this line + let fast = head + let slow = head + + if (head.next == null) { return head } + + while (fast != null && fast.next != null) { + fast = fast.next.next + slow = slow.next + } + return slow +} + +main() From b0ddd37136fadd9559a95c599803bfc22838d8f6 Mon Sep 17 00:00:00 2001 From: 10kartik Date: Fri, 9 Sep 2022 12:16:26 +0530 Subject: [PATCH 2/5] Added Middle of LL function and tests --- .../Linked-List/MiddleOfLinkedList.js | 29 ++++++------- .../test/MiddleOfLinkedList.test.js | 41 +++++++++++++++++++ 2 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 Data-Structures/Linked-List/test/MiddleOfLinkedList.test.js diff --git a/Data-Structures/Linked-List/MiddleOfLinkedList.js b/Data-Structures/Linked-List/MiddleOfLinkedList.js index b6eaa62865..7991581e70 100644 --- a/Data-Structures/Linked-List/MiddleOfLinkedList.js +++ b/Data-Structures/Linked-List/MiddleOfLinkedList.js @@ -1,32 +1,27 @@ /** - * A LinkedList based solution for Detect a Cycle in a list + * A LinkedList based solution for finding middle node of linked list. * https://afteracademy.com/blog/middle-of-the-linked-list */ - -function main () { +class MiddleOfLL { + solution (head) { /* Problem Statement: Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node. - Note: - * While Solving the problem in given link below, don't use main() function. - * Just use only the code inside main() function. - * The purpose of using main() function here is to avoid global variables. - Link for the Problem: https://leetcode.com/problems/middle-of-the-linked-list/ */ - const head = '' // Reference to head is given in the problem. So please ignore this line - let fast = head - let slow = head + let fast = head + let slow = head - if (head.next == null) { return head } + if (head.next == null) { return head } - while (fast != null && fast.next != null) { - fast = fast.next.next - slow = slow.next + while (fast != null && fast.next != null) { + fast = fast.next.next + slow = slow.next + } + return slow } - return slow } -main() +export { MiddleOfLL } diff --git a/Data-Structures/Linked-List/test/MiddleOfLinkedList.test.js b/Data-Structures/Linked-List/test/MiddleOfLinkedList.test.js new file mode 100644 index 0000000000..78e7600d3c --- /dev/null +++ b/Data-Structures/Linked-List/test/MiddleOfLinkedList.test.js @@ -0,0 +1,41 @@ +import { MiddleOfLL } from '../MiddleOfLinkedList' +import { LinkedList } from '../SinglyLinkedList' + +describe('MiddleOfLinkedList', () => { + it('middle node Of linked list - even length ', () => { + const list = new LinkedList() + list.addFirst(1) + list.addLast(2) + list.addLast(3) + list.addLast(4) + list.addLast(5) + list.addLast(6) + list.addLast(7) + + const MiddleNodeOfLinkedList = new MiddleOfLL().solution(list.headNode) + expect(MiddleNodeOfLinkedList.data).toEqual(4) + }) + + it('middle node of linked list - odd length ', () => { + const list = new LinkedList() + list.addFirst(10) + list.addLast(20) + list.addLast(30) + list.addLast(40) + list.addLast(50) + list.addLast(60) + list.addLast(70) + list.addLast(80) + + const MiddleNodeOfLinkedList = new MiddleOfLL().solution(list.headNode) + expect(MiddleNodeOfLinkedList.data).toEqual(50) + }) + + it('middle node of linked list - length 1 ', () => { + const list = new LinkedList() + list.addFirst(100) + + const MiddleNodeOfLinkedList = new MiddleOfLL().solution(list.headNode) + expect(MiddleNodeOfLinkedList.data).toEqual(100) + }) +}) From a70a3175a8b94e4b3a8539f291adb6d30330370f Mon Sep 17 00:00:00 2001 From: 10kartik Date: Fri, 9 Sep 2022 13:58:41 +0530 Subject: [PATCH 3/5] Refactor: Added method in singly LL and its tests --- .../Linked-List/MiddleOfLinkedList.js | 27 ------------ .../Linked-List/SinglyLinkedList.js | 13 ++++++ .../test/MiddleOfLinkedList.test.js | 41 ------------------- .../Linked-List/test/SinglyLinkedList.test.js | 26 ++++++++++++ 4 files changed, 39 insertions(+), 68 deletions(-) delete mode 100644 Data-Structures/Linked-List/MiddleOfLinkedList.js delete mode 100644 Data-Structures/Linked-List/test/MiddleOfLinkedList.test.js diff --git a/Data-Structures/Linked-List/MiddleOfLinkedList.js b/Data-Structures/Linked-List/MiddleOfLinkedList.js deleted file mode 100644 index 7991581e70..0000000000 --- a/Data-Structures/Linked-List/MiddleOfLinkedList.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * A LinkedList based solution for finding middle node of linked list. - * https://afteracademy.com/blog/middle-of-the-linked-list - */ -class MiddleOfLL { - solution (head) { - /* - Problem Statement: - Given the head of a singly linked list, return the middle node of the linked list. - If there are two middle nodes, return the second middle node. - - Link for the Problem: https://leetcode.com/problems/middle-of-the-linked-list/ - */ - let fast = head - let slow = head - - if (head.next == null) { return head } - - while (fast != null && fast.next != null) { - fast = fast.next.next - slow = slow.next - } - return slow - } -} - -export { MiddleOfLL } diff --git a/Data-Structures/Linked-List/SinglyLinkedList.js b/Data-Structures/Linked-List/SinglyLinkedList.js index f591be13c9..081b4af9cf 100644 --- a/Data-Structures/Linked-List/SinglyLinkedList.js +++ b/Data-Structures/Linked-List/SinglyLinkedList.js @@ -193,6 +193,19 @@ class LinkedList { return removedNode.data } + // Returns a reference to middle node of linked list + MiddleOfLL () { + // If there are two middle nodes, return the second middle node. + let fast = this.headNode + let slow = this.headNode + + while (fast != null && fast.next != null) { + fast = fast.next.next + slow = slow.next + } + return slow + } + // make the linkedList Empty clean () { this.headNode = null diff --git a/Data-Structures/Linked-List/test/MiddleOfLinkedList.test.js b/Data-Structures/Linked-List/test/MiddleOfLinkedList.test.js deleted file mode 100644 index 78e7600d3c..0000000000 --- a/Data-Structures/Linked-List/test/MiddleOfLinkedList.test.js +++ /dev/null @@ -1,41 +0,0 @@ -import { MiddleOfLL } from '../MiddleOfLinkedList' -import { LinkedList } from '../SinglyLinkedList' - -describe('MiddleOfLinkedList', () => { - it('middle node Of linked list - even length ', () => { - const list = new LinkedList() - list.addFirst(1) - list.addLast(2) - list.addLast(3) - list.addLast(4) - list.addLast(5) - list.addLast(6) - list.addLast(7) - - const MiddleNodeOfLinkedList = new MiddleOfLL().solution(list.headNode) - expect(MiddleNodeOfLinkedList.data).toEqual(4) - }) - - it('middle node of linked list - odd length ', () => { - const list = new LinkedList() - list.addFirst(10) - list.addLast(20) - list.addLast(30) - list.addLast(40) - list.addLast(50) - list.addLast(60) - list.addLast(70) - list.addLast(80) - - const MiddleNodeOfLinkedList = new MiddleOfLL().solution(list.headNode) - expect(MiddleNodeOfLinkedList.data).toEqual(50) - }) - - it('middle node of linked list - length 1 ', () => { - const list = new LinkedList() - list.addFirst(100) - - const MiddleNodeOfLinkedList = new MiddleOfLL().solution(list.headNode) - expect(MiddleNodeOfLinkedList.data).toEqual(100) - }) -}) diff --git a/Data-Structures/Linked-List/test/SinglyLinkedList.test.js b/Data-Structures/Linked-List/test/SinglyLinkedList.test.js index 4bfde62c84..6f3b02f9ea 100644 --- a/Data-Structures/Linked-List/test/SinglyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/SinglyLinkedList.test.js @@ -164,6 +164,32 @@ describe('SinglyLinkedList', () => { expect(list.size()).toBe(1) }) + it('Middle node of linked list', () => { + const list = new LinkedList() + list.addFirst(1) + + let MiddleNodeOfLinkedList = list.MiddleOfLL(list.headNode) + // Middle node for list having single node + expect(MiddleNodeOfLinkedList.data).toEqual(1) + + list.addLast(2) + list.addLast(3) + list.addLast(4) + list.addLast(5) + list.addLast(6) + list.addLast(7) + + MiddleNodeOfLinkedList = list.MiddleOfLL(list.headNode) + // Middle node for list having odd number of nodes + expect(MiddleNodeOfLinkedList.data).toEqual(4) + + list.addLast(10) + + MiddleNodeOfLinkedList = list.MiddleOfLL(list.headNode) + // Middle node for list having even number of nodes + expect(MiddleNodeOfLinkedList.data).toEqual(5) + }) + it('Check Iterator', () => { const list = new LinkedList() From fbce118f785773393598b991dc9c9a3d987735a9 Mon Sep 17 00:00:00 2001 From: 10kartik Date: Fri, 9 Sep 2022 14:23:37 +0530 Subject: [PATCH 4/5] Refactor: Method name and inline test calls --- Data-Structures/Linked-List/SinglyLinkedList.js | 4 ++-- .../Linked-List/test/SinglyLinkedList.test.js | 9 +++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Data-Structures/Linked-List/SinglyLinkedList.js b/Data-Structures/Linked-List/SinglyLinkedList.js index 081b4af9cf..14673c4317 100644 --- a/Data-Structures/Linked-List/SinglyLinkedList.js +++ b/Data-Structures/Linked-List/SinglyLinkedList.js @@ -6,7 +6,7 @@ * a singly linked list. */ -// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, get, clean +// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, findMiddle, get, clean class Node { constructor (data) { @@ -194,7 +194,7 @@ class LinkedList { } // Returns a reference to middle node of linked list - MiddleOfLL () { + findMiddle () { // If there are two middle nodes, return the second middle node. let fast = this.headNode let slow = this.headNode diff --git a/Data-Structures/Linked-List/test/SinglyLinkedList.test.js b/Data-Structures/Linked-List/test/SinglyLinkedList.test.js index 6f3b02f9ea..bb0124226d 100644 --- a/Data-Structures/Linked-List/test/SinglyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/SinglyLinkedList.test.js @@ -168,9 +168,8 @@ describe('SinglyLinkedList', () => { const list = new LinkedList() list.addFirst(1) - let MiddleNodeOfLinkedList = list.MiddleOfLL(list.headNode) // Middle node for list having single node - expect(MiddleNodeOfLinkedList.data).toEqual(1) + expect(list.findMiddle().data).toEqual(1) list.addLast(2) list.addLast(3) @@ -179,15 +178,13 @@ describe('SinglyLinkedList', () => { list.addLast(6) list.addLast(7) - MiddleNodeOfLinkedList = list.MiddleOfLL(list.headNode) // Middle node for list having odd number of nodes - expect(MiddleNodeOfLinkedList.data).toEqual(4) + expect(list.findMiddle().data).toEqual(4) list.addLast(10) - MiddleNodeOfLinkedList = list.MiddleOfLL(list.headNode) // Middle node for list having even number of nodes - expect(MiddleNodeOfLinkedList.data).toEqual(5) + expect(list.findMiddle().data).toEqual(5) }) it('Check Iterator', () => { From fdeca6a29cfdfe97e52cbaf1ebc613e6379b36f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Fri, 9 Sep 2022 10:59:03 +0200 Subject: [PATCH 5/5] Use `!== null` instead of `!= null` --- Data-Structures/Linked-List/SinglyLinkedList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data-Structures/Linked-List/SinglyLinkedList.js b/Data-Structures/Linked-List/SinglyLinkedList.js index 14673c4317..c7a803f53c 100644 --- a/Data-Structures/Linked-List/SinglyLinkedList.js +++ b/Data-Structures/Linked-List/SinglyLinkedList.js @@ -199,7 +199,7 @@ class LinkedList { let fast = this.headNode let slow = this.headNode - while (fast != null && fast.next != null) { + while (fast !== null && fast.next !== null) { fast = fast.next.next slow = slow.next }