Skip to content

Commit ba122e4

Browse files
committed
refactor: Enhance docs, add tests in RotateSinglyLinkedLists
1 parent 0f1dcbe commit ba122e4

File tree

2 files changed

+105
-41
lines changed

2 files changed

+105
-41
lines changed

src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
11
package com.thealgorithms.datastructures.lists;
22

33
/**
4-
* Rotate a list
5-
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
4+
* The RotateSinglyLinkedLists class provides a method to rotate a singly linked list
5+
* to the right by a specified number of positions.
6+
* <p>
7+
* In a right rotation by `k` steps, each node in the list moves `k` positions to the right.
8+
* Nodes that are rotated off the end of the list are placed back at the beginning.
9+
* </p>
10+
* <p>
11+
* Example:
12+
* Given linked list: 1 -> 2 -> 3 -> 4 -> 5 and k = 2, the output will be:
13+
* 4 -> 5 -> 1 -> 2 -> 3.
14+
* </p>
15+
* <p>
16+
* Edge Cases:
17+
* <ul>
18+
* <li>If the list is empty, returns null.</li>
19+
* <li>If `k` is 0 or a multiple of the list length, the list remains unchanged.</li>
20+
* </ul>
21+
* </p>
22+
* <p>
23+
* Complexity:
24+
* <ul>
25+
* <li>Time Complexity: O(n), where n is the number of nodes in the linked list.</li>
26+
* <li>Space Complexity: O(1), as we only use a constant amount of additional space.</li>
27+
* </ul>
28+
* </p>
29+
*
30+
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
631
*/
7-
832
public class RotateSinglyLinkedLists {
33+
34+
/**
35+
* Rotates a singly linked list to the right by `k` positions.
36+
*
37+
* @param head The head node of the singly linked list.
38+
* @param k The number of positions to rotate the list to the right.
39+
* @return The head of the rotated linked list.
40+
*/
941
public Node rotateRight(Node head, int k) {
1042
if (head == null || head.next == null || k == 0) {
1143
return head;

src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,99 @@
66
import org.junit.jupiter.api.Test;
77

88
/**
9-
* Test cases for RotateSinglyLinkedLists
9+
* Test cases for RotateSinglyLinkedLists.
1010
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
1111
*/
1212
public class RotateSinglyLinkedListsTest {
1313

14+
private final RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
15+
16+
// Helper method to create a linked list from an array of values
17+
private Node createLinkedList(int[] values) {
18+
if (values.length == 0) return null;
19+
20+
Node head = new Node(values[0]);
21+
Node current = head;
22+
for (int i = 1; i < values.length; i++) {
23+
current.next = new Node(values[i]);
24+
current = current.next;
25+
}
26+
return head;
27+
}
28+
29+
// Helper method to convert a linked list to a string for easy comparison
30+
private String linkedListToString(Node head) {
31+
StringBuilder sb = new StringBuilder();
32+
Node current = head;
33+
while (current != null) {
34+
sb.append(current.value);
35+
if (current.next != null) {
36+
sb.append(" -> ");
37+
}
38+
current = current.next;
39+
}
40+
return sb.toString();
41+
}
42+
1443
@Test
1544
public void testRotateRightEmptyList() {
16-
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
17-
18-
// Test case: Rotate an empty list
45+
// Rotate an empty list
1946
assertNull(rotator.rotateRight(null, 2));
2047
}
2148

2249
@Test
2350
public void testRotateRightSingleNodeList() {
24-
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
25-
26-
// Test case: Rotate a list with one element
51+
// Rotate a list with a single element
2752
Node singleNode = new Node(5);
2853
Node rotatedSingleNode = rotator.rotateRight(singleNode, 3);
29-
assertEquals(5, rotatedSingleNode.value);
30-
assertNull(rotatedSingleNode.next);
54+
assertEquals("5", linkedListToString(rotatedSingleNode));
3155
}
3256

3357
@Test
3458
public void testRotateRightMultipleElementsList() {
35-
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
59+
// Rotate a list with multiple elements (rotate by 2)
60+
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
61+
Node rotated = rotator.rotateRight(head, 2);
62+
assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated));
63+
}
3664

37-
// Test case: Rotate a list with multiple elements (Rotate by 2)
38-
Node head = new Node(1);
39-
head.next = new Node(2);
40-
head.next.next = new Node(3);
41-
head.next.next.next = new Node(4);
42-
head.next.next.next.next = new Node(5);
65+
@Test
66+
public void testRotateRightFullRotation() {
67+
// Rotate by more than the length of the list
68+
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
69+
Node rotated = rotator.rotateRight(head, 7);
70+
assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated));
71+
}
4372

44-
Node rotated1 = rotator.rotateRight(head, 2);
45-
assertEquals(4, rotated1.value);
46-
assertEquals(5, rotated1.next.value);
47-
assertEquals(1, rotated1.next.next.value);
48-
assertEquals(2, rotated1.next.next.next.value);
49-
assertEquals(3, rotated1.next.next.next.next.value);
50-
assertNull(rotated1.next.next.next.next.next);
73+
@Test
74+
public void testRotateRightZeroRotation() {
75+
// Rotate a list by k = 0 (no rotation)
76+
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
77+
Node rotated = rotator.rotateRight(head, 0);
78+
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
5179
}
5280

5381
@Test
54-
public void testRotateRightFullRotation() {
55-
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
82+
public void testRotateRightByListLength() {
83+
// Rotate a list by k equal to list length (no change)
84+
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
85+
Node rotated = rotator.rotateRight(head, 5);
86+
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
87+
}
5688

57-
// Test case: Rotate a list with multiple elements (Full rotation)
58-
Node head = new Node(1);
59-
head.next = new Node(2);
60-
head.next.next = new Node(3);
61-
head.next.next.next = new Node(4);
62-
head.next.next.next.next = new Node(5);
89+
@Test
90+
public void testRotateRightByMultipleOfListLength() {
91+
// Rotate a list by a multiple of its length (no change)
92+
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
93+
Node rotated = rotator.rotateRight(head, 10); // k = 2 * list length
94+
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
95+
}
6396

64-
Node rotated3 = rotator.rotateRight(head, 7);
65-
assertEquals(4, rotated3.value);
66-
assertEquals(5, rotated3.next.value);
67-
assertEquals(1, rotated3.next.next.value);
68-
assertEquals(2, rotated3.next.next.next.value);
69-
assertEquals(3, rotated3.next.next.next.next.value);
70-
assertNull(rotated3.next.next.next.next.next);
97+
@Test
98+
public void testRotateRightLongerList() {
99+
// Rotate a longer list by a smaller k
100+
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
101+
Node rotated = rotator.rotateRight(head, 4);
102+
assertEquals("6 -> 7 -> 8 -> 9 -> 1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
71103
}
72104
}

0 commit comments

Comments
 (0)