Skip to content

Commit 455e07a

Browse files
author
10kartik
committed
Refactored RotatedListRight.js and added its tests
1 parent e7ee09a commit 455e07a

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

Data-Structures/Linked-List/RotateListRight.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22
* A LinkedList based solution for Rotating a List to the right by k places
33
*/
44

5-
function main () {
5+
function rotateListRight (head, k) {
66
/*
77
Problem Statement:
88
Given a linked list, rotate the list to the right by k places, where k is non-negative.
99
10-
Note:
11-
* While Solving the problem in given link below, don't use main() function.
12-
* Just use only the code inside main() function.
13-
* The purpose of using main() function here is to avoid global variables.
14-
1510
Link for the Problem: https://leetcode.com/problems/rotate-list/
1611
*/
17-
// Reference to both head and k is given in the problem. So please ignore below two lines
18-
let head = ''
19-
let k = ''
2012
let i = 0
2113
let current = head
2214
while (current) {
@@ -42,4 +34,4 @@ function main () {
4234
return head
4335
}
4436

45-
main()
37+
export { rotateListRight }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { rotateListRight } from '../RotateListRight'
2+
import { Node } from '../SinglyLinkedList'
3+
4+
describe('Rotate list by k steps', () => {
5+
it('should shift every node by k steps towards right, shifts few tail nodes towards the start and change head of the list', () => {
6+
// Case 0: when head is null
7+
let headNode = rotateListRight(null, 0)
8+
expect(headNode).toEqual(null)
9+
10+
// Creating list
11+
headNode = new Node(10)
12+
headNode.next = new Node(20)
13+
headNode.next.next = new Node(30)
14+
headNode.next.next.next = new Node(40)
15+
headNode.next.next.next.next = new Node(50)
16+
17+
// Case 1: when k = 0 => List should be unaffected
18+
headNode = rotateListRight(headNode, 0)
19+
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])
20+
21+
// Case 2: Rotate right by 2 steps
22+
headNode = rotateListRight(headNode, 2)
23+
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])
24+
25+
// Case 3: Rotate right by 12 steps
26+
headNode = rotateListRight(headNode, 12)
27+
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])
28+
29+
// Case 4: when k = length of the list = 5 => List should be unaffected
30+
headNode = rotateListRight(headNode, 5)
31+
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])
32+
})
33+
})

0 commit comments

Comments
 (0)