Skip to content

Commit 9bcf16b

Browse files
authored
refactor: RotateListRight.js and added tests (TheAlgorithms#1101)
* Refactored RotatedListRight.js and added its tests * rotateListRight test and improved implementation * Review changes on constructor's loop
1 parent d05bbf7 commit 9bcf16b

File tree

3 files changed

+59
-47
lines changed

3 files changed

+59
-47
lines changed

Data-Structures/Linked-List/RotateListRight.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

Data-Structures/Linked-List/SinglyLinkedList.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* a singly linked list.
77
*/
88

9-
// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, findMiddle, get, clean
9+
// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, findMiddle, get, clean, rotateListRight
1010

1111
class Node {
1212
constructor (data) {
@@ -16,9 +16,15 @@ class Node {
1616
}
1717

1818
class LinkedList {
19-
constructor () {
19+
constructor (listOfValues) {
2020
this.headNode = null
2121
this.length = 0
22+
23+
if (listOfValues instanceof Array) {
24+
for (const value of listOfValues) {
25+
this.addLast(value)
26+
}
27+
}
2228
}
2329

2430
// initiates the currentNode and currentIndex and return as an object
@@ -224,6 +230,32 @@ class LinkedList {
224230
return list
225231
}
226232

233+
// Method for Rotating a List to the right by k places
234+
rotateListRight (k) {
235+
let i = 0
236+
let current = this.headNode
237+
while (current) {
238+
i++
239+
current = current.next
240+
}
241+
k %= i
242+
current = this.headNode
243+
let prev = null
244+
while (k--) {
245+
if (!current || !current.next) {
246+
return current
247+
} else {
248+
while (current.next) {
249+
prev = current
250+
current = current.next
251+
}
252+
prev.next = current.next
253+
current.next = this.headNode
254+
this.headNode = current
255+
}
256+
}
257+
}
258+
227259
// Method to iterate over the LinkedList
228260
iterator () {
229261
let { currentNode } = this.initiateNodeAndIndex()

Data-Structures/Linked-List/test/SinglyLinkedList.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,29 @@ describe('SinglyLinkedList', () => {
222222
list.clean()
223223
expect(list.isEmpty()).toBe(true)
224224
})
225+
226+
it('should shift every node by k steps towards right, shifts tail nodes towards the start and change head of the list', () => {
227+
// Case 0: When head of list is null
228+
const tempNode = new LinkedList()
229+
expect(tempNode.get()).toEqual([])
230+
231+
// Creating list
232+
const headNode = new LinkedList([10, 20, 30, 40, 50])
233+
234+
// Case 1: when k = 0 => List should be unaffected
235+
headNode.rotateListRight(0)
236+
expect(headNode.get()).toEqual([10, 20, 30, 40, 50])
237+
238+
// Case 2: Rotate right by 2 steps
239+
headNode.rotateListRight(2)
240+
expect(headNode.get()).toEqual([40, 50, 10, 20, 30])
241+
242+
// Case 3: Rotate right by 12 steps
243+
headNode.rotateListRight(12)
244+
expect(headNode.get()).toEqual([20, 30, 40, 50, 10])
245+
246+
// Case 4: when k = length of the list = 5 => List should be unaffected
247+
headNode.rotateListRight(5)
248+
expect(headNode.get()).toEqual([20, 30, 40, 50, 10])
249+
})
225250
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy