From 455e07a9d159134cd88703eab1b0c6161c05921b Mon Sep 17 00:00:00 2001 From: 10kartik Date: Wed, 14 Sep 2022 20:18:42 +0530 Subject: [PATCH 1/3] Refactored RotatedListRight.js and added its tests --- .../Linked-List/RotateListRight.js | 12 ++----- .../Linked-List/test/RotateListRight.test.js | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 Data-Structures/Linked-List/test/RotateListRight.test.js diff --git a/Data-Structures/Linked-List/RotateListRight.js b/Data-Structures/Linked-List/RotateListRight.js index 5fbdc913e4..446a725c60 100644 --- a/Data-Structures/Linked-List/RotateListRight.js +++ b/Data-Structures/Linked-List/RotateListRight.js @@ -2,21 +2,13 @@ * A LinkedList based solution for Rotating a List to the right by k places */ -function main () { +function rotateListRight (head, k) { /* Problem Statement: Given a linked list, rotate the list to the right by k places, where k is non-negative. - 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/rotate-list/ */ - // Reference to both head and k is given in the problem. So please ignore below two lines - let head = '' - let k = '' let i = 0 let current = head while (current) { @@ -42,4 +34,4 @@ function main () { return head } -main() +export { rotateListRight } diff --git a/Data-Structures/Linked-List/test/RotateListRight.test.js b/Data-Structures/Linked-List/test/RotateListRight.test.js new file mode 100644 index 0000000000..e83507fa7b --- /dev/null +++ b/Data-Structures/Linked-List/test/RotateListRight.test.js @@ -0,0 +1,33 @@ +import { rotateListRight } from '../RotateListRight' +import { Node } from '../SinglyLinkedList' + +describe('Rotate list by k steps', () => { + it('should shift every node by k steps towards right, shifts few tail nodes towards the start and change head of the list', () => { + // Case 0: when head is null + let headNode = rotateListRight(null, 0) + expect(headNode).toEqual(null) + + // Creating list + headNode = new Node(10) + headNode.next = new Node(20) + headNode.next.next = new Node(30) + headNode.next.next.next = new Node(40) + headNode.next.next.next.next = new Node(50) + + // Case 1: when k = 0 => List should be unaffected + headNode = rotateListRight(headNode, 0) + expect([headNode.data, headNode.next.data, headNode.next.next.data, headNode.next.next.next.data, headNode.next.next.next.next.data]).toEqual([10, 20, 30, 40, 50]) + + // Case 2: Rotate right by 2 steps + headNode = rotateListRight(headNode, 2) + expect([headNode.data, headNode.next.data, headNode.next.next.data, headNode.next.next.next.data, headNode.next.next.next.next.data]).toEqual([40, 50, 10, 20, 30]) + + // Case 3: Rotate right by 12 steps + headNode = rotateListRight(headNode, 12) + expect([headNode.data, headNode.next.data, headNode.next.next.data, headNode.next.next.next.data, headNode.next.next.next.next.data]).toEqual([20, 30, 40, 50, 10]) + + // Case 4: when k = length of the list = 5 => List should be unaffected + headNode = rotateListRight(headNode, 5) + expect([headNode.data, headNode.next.data, headNode.next.next.data, headNode.next.next.next.data, headNode.next.next.next.next.data]).toEqual([20, 30, 40, 50, 10]) + }) +}) From 7ccde3f743fac73a3d02abac43d336e5c994fa27 Mon Sep 17 00:00:00 2001 From: 10kartik Date: Mon, 19 Sep 2022 18:52:04 +0530 Subject: [PATCH 2/3] rotateListRight test and improved implementation --- .../Linked-List/RotateListRight.js | 37 ------------------- .../Linked-List/SinglyLinkedList.js | 36 +++++++++++++++++- .../Linked-List/test/RotateListRight.test.js | 33 ----------------- .../Linked-List/test/SinglyLinkedList.test.js | 25 +++++++++++++ 4 files changed, 59 insertions(+), 72 deletions(-) delete mode 100644 Data-Structures/Linked-List/RotateListRight.js delete mode 100644 Data-Structures/Linked-List/test/RotateListRight.test.js diff --git a/Data-Structures/Linked-List/RotateListRight.js b/Data-Structures/Linked-List/RotateListRight.js deleted file mode 100644 index 446a725c60..0000000000 --- a/Data-Structures/Linked-List/RotateListRight.js +++ /dev/null @@ -1,37 +0,0 @@ -/** - * A LinkedList based solution for Rotating a List to the right by k places - */ - -function rotateListRight (head, k) { - /* - Problem Statement: - Given a linked list, rotate the list to the right by k places, where k is non-negative. - - Link for the Problem: https://leetcode.com/problems/rotate-list/ - */ - let i = 0 - let current = head - while (current) { - i++ - current = current.next - } - k %= i - current = head - let prev = null - while (k--) { - if (!current || !current.next) { - return current - } else { - while (current.next) { - prev = current - current = current.next - } - prev.next = current.next - current.next = head - head = current - } - } - return head -} - -export { rotateListRight } diff --git a/Data-Structures/Linked-List/SinglyLinkedList.js b/Data-Structures/Linked-List/SinglyLinkedList.js index c7a803f53c..f9211438aa 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, findMiddle, get, clean +// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, findMiddle, get, clean, rotateListRight class Node { constructor (data) { @@ -16,9 +16,15 @@ class Node { } class LinkedList { - constructor () { + constructor (listOfValues) { this.headNode = null this.length = 0 + + if (listOfValues instanceof Array) { + for (let i = 0; i < listOfValues.length; i++) { + this.addLast(listOfValues[i]) + } + } } // initiates the currentNode and currentIndex and return as an object @@ -224,6 +230,32 @@ class LinkedList { return list } + // Method for Rotating a List to the right by k places + rotateListRight (k) { + let i = 0 + let current = this.headNode + while (current) { + i++ + current = current.next + } + k %= i + current = this.headNode + let prev = null + while (k--) { + if (!current || !current.next) { + return current + } else { + while (current.next) { + prev = current + current = current.next + } + prev.next = current.next + current.next = this.headNode + this.headNode = current + } + } + } + // Method to iterate over the LinkedList iterator () { let { currentNode } = this.initiateNodeAndIndex() diff --git a/Data-Structures/Linked-List/test/RotateListRight.test.js b/Data-Structures/Linked-List/test/RotateListRight.test.js deleted file mode 100644 index e83507fa7b..0000000000 --- a/Data-Structures/Linked-List/test/RotateListRight.test.js +++ /dev/null @@ -1,33 +0,0 @@ -import { rotateListRight } from '../RotateListRight' -import { Node } from '../SinglyLinkedList' - -describe('Rotate list by k steps', () => { - it('should shift every node by k steps towards right, shifts few tail nodes towards the start and change head of the list', () => { - // Case 0: when head is null - let headNode = rotateListRight(null, 0) - expect(headNode).toEqual(null) - - // Creating list - headNode = new Node(10) - headNode.next = new Node(20) - headNode.next.next = new Node(30) - headNode.next.next.next = new Node(40) - headNode.next.next.next.next = new Node(50) - - // Case 1: when k = 0 => List should be unaffected - headNode = rotateListRight(headNode, 0) - expect([headNode.data, headNode.next.data, headNode.next.next.data, headNode.next.next.next.data, headNode.next.next.next.next.data]).toEqual([10, 20, 30, 40, 50]) - - // Case 2: Rotate right by 2 steps - headNode = rotateListRight(headNode, 2) - expect([headNode.data, headNode.next.data, headNode.next.next.data, headNode.next.next.next.data, headNode.next.next.next.next.data]).toEqual([40, 50, 10, 20, 30]) - - // Case 3: Rotate right by 12 steps - headNode = rotateListRight(headNode, 12) - expect([headNode.data, headNode.next.data, headNode.next.next.data, headNode.next.next.next.data, headNode.next.next.next.next.data]).toEqual([20, 30, 40, 50, 10]) - - // Case 4: when k = length of the list = 5 => List should be unaffected - headNode = rotateListRight(headNode, 5) - expect([headNode.data, headNode.next.data, headNode.next.next.data, headNode.next.next.next.data, headNode.next.next.next.next.data]).toEqual([20, 30, 40, 50, 10]) - }) -}) diff --git a/Data-Structures/Linked-List/test/SinglyLinkedList.test.js b/Data-Structures/Linked-List/test/SinglyLinkedList.test.js index bb0124226d..764cac7aa9 100644 --- a/Data-Structures/Linked-List/test/SinglyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/SinglyLinkedList.test.js @@ -222,4 +222,29 @@ describe('SinglyLinkedList', () => { list.clean() expect(list.isEmpty()).toBe(true) }) + + it('should shift every node by k steps towards right, shifts tail nodes towards the start and change head of the list', () => { + // Case 0: When head of list is null + const tempNode = new LinkedList() + expect(tempNode.get()).toEqual([]) + + // Creating list + const headNode = new LinkedList([10, 20, 30, 40, 50]) + + // Case 1: when k = 0 => List should be unaffected + headNode.rotateListRight(0) + expect(headNode.get()).toEqual([10, 20, 30, 40, 50]) + + // Case 2: Rotate right by 2 steps + headNode.rotateListRight(2) + expect(headNode.get()).toEqual([40, 50, 10, 20, 30]) + + // Case 3: Rotate right by 12 steps + headNode.rotateListRight(12) + expect(headNode.get()).toEqual([20, 30, 40, 50, 10]) + + // Case 4: when k = length of the list = 5 => List should be unaffected + headNode.rotateListRight(5) + expect(headNode.get()).toEqual([20, 30, 40, 50, 10]) + }) }) From 9797a43c97e089efda1bfc8c291318574697a718 Mon Sep 17 00:00:00 2001 From: 10kartik Date: Mon, 19 Sep 2022 23:48:12 +0530 Subject: [PATCH 3/3] Review changes on constructor's loop --- Data-Structures/Linked-List/SinglyLinkedList.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Data-Structures/Linked-List/SinglyLinkedList.js b/Data-Structures/Linked-List/SinglyLinkedList.js index f9211438aa..d55e0b14b8 100644 --- a/Data-Structures/Linked-List/SinglyLinkedList.js +++ b/Data-Structures/Linked-List/SinglyLinkedList.js @@ -21,8 +21,8 @@ class LinkedList { this.length = 0 if (listOfValues instanceof Array) { - for (let i = 0; i < listOfValues.length; i++) { - this.addLast(listOfValues[i]) + for (const value of listOfValues) { + this.addLast(value) } } }