diff --git a/Data-Structures/Linked-List/RotateListRight.js b/Data-Structures/Linked-List/RotateListRight.js deleted file mode 100644 index 5fbdc913e4..0000000000 --- a/Data-Structures/Linked-List/RotateListRight.js +++ /dev/null @@ -1,45 +0,0 @@ -/** - * A LinkedList based solution for Rotating a List to the right by k places - */ - -function main () { - /* - 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) { - 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 -} - -main() diff --git a/Data-Structures/Linked-List/SinglyLinkedList.js b/Data-Structures/Linked-List/SinglyLinkedList.js index c7a803f53c..d55e0b14b8 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 (const value of listOfValues) { + this.addLast(value) + } + } } // 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/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]) + }) })