refactor: RotateListRight.js and added tests (#1101)

* Refactored RotatedListRight.js and added its tests

* rotateListRight test and improved implementation

* Review changes on constructor's loop
This commit is contained in:
Kartik Kapgate
2022-09-22 17:22:11 +05:30
committed by GitHub
parent d05bbf77c6
commit 9bcf16ba4b
3 changed files with 59 additions and 47 deletions

View File

@ -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()

View File

@ -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()

View File

@ -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])
})
})