Skip to content

Commit 9dbdaf6

Browse files
authored
Enhance docs, add tests in RotateSinglyLinkedLists (#6011)
1 parent e1a0155 commit 9dbdaf6

File tree

2 files changed

+106
-41
lines changed

2 files changed

+106
-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: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,100 @@
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) {
19+
return null;
20+
}
21+
22+
Node head = new Node(values[0]);
23+
Node current = head;
24+
for (int i = 1; i < values.length; i++) {
25+
current.next = new Node(values[i]);
26+
current = current.next;
27+
}
28+
return head;
29+
}
30+
31+
// Helper method to convert a linked list to a string for easy comparison
32+
private String linkedListToString(Node head) {
33+
StringBuilder sb = new StringBuilder();
34+
Node current = head;
35+
while (current != null) {
36+
sb.append(current.value);
37+
if (current.next != null) {
38+
sb.append(" -> ");
39+
}
40+
current = current.next;
41+
}
42+
return sb.toString();
43+
}
44+
1445
@Test
1546
public void testRotateRightEmptyList() {
16-
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
17-
18-
// Test case: Rotate an empty list
47+
// Rotate an empty list
1948
assertNull(rotator.rotateRight(null, 2));
2049
}
2150

2251
@Test
2352
public void testRotateRightSingleNodeList() {
24-
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
25-
26-
// Test case: Rotate a list with one element
53+
// Rotate a list with a single element
2754
Node singleNode = new Node(5);
2855
Node rotatedSingleNode = rotator.rotateRight(singleNode, 3);
29-
assertEquals(5, rotatedSingleNode.value);
30-
assertNull(rotatedSingleNode.next);
56+
assertEquals("5", linkedListToString(rotatedSingleNode));
3157
}
3258

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

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);
67+
@Test
68+
public void testRotateRightFullRotation() {
69+
// Rotate by more than the length of the list
70+
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
71+
Node rotated = rotator.rotateRight(head, 7);
72+
assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated));
73+
}
4374

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);
75+
@Test
76+
public void testRotateRightZeroRotation() {
77+
// Rotate a list by k = 0 (no rotation)
78+
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
79+
Node rotated = rotator.rotateRight(head, 0);
80+
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
5181
}
5282

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

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);
91+
@Test
92+
public void testRotateRightByMultipleOfListLength() {
93+
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
94+
Node rotated = rotator.rotateRight(head, 10); // k = 2 * list length
95+
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
96+
}
6397

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);
98+
@Test
99+
public void testRotateRightLongerList() {
100+
// Rotate a longer list by a smaller k
101+
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
102+
Node rotated = rotator.rotateRight(head, 4);
103+
assertEquals("6 -> 7 -> 8 -> 9 -> 1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
71104
}
72105
}

0 commit comments

Comments
 (0)