mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
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:
@ -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()
|
|
@ -6,7 +6,7 @@
|
|||||||
* a singly linked list.
|
* 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 {
|
class Node {
|
||||||
constructor (data) {
|
constructor (data) {
|
||||||
@ -16,9 +16,15 @@ class Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class LinkedList {
|
class LinkedList {
|
||||||
constructor () {
|
constructor (listOfValues) {
|
||||||
this.headNode = null
|
this.headNode = null
|
||||||
this.length = 0
|
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
|
// initiates the currentNode and currentIndex and return as an object
|
||||||
@ -224,6 +230,32 @@ class LinkedList {
|
|||||||
return list
|
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
|
// Method to iterate over the LinkedList
|
||||||
iterator () {
|
iterator () {
|
||||||
let { currentNode } = this.initiateNodeAndIndex()
|
let { currentNode } = this.initiateNodeAndIndex()
|
||||||
|
@ -222,4 +222,29 @@ describe('SinglyLinkedList', () => {
|
|||||||
list.clean()
|
list.clean()
|
||||||
expect(list.isEmpty()).toBe(true)
|
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])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user