From ba122e46c30abef0e5395249052a3f32e34ffc5b Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sat, 26 Oct 2024 09:10:03 +0530 Subject: [PATCH 1/4] refactor: Enhance docs, add tests in `RotateSinglyLinkedLists` --- .../lists/RotateSinglyLinkedLists.java | 38 +++++- .../lists/RotateSinglyLinkedListsTest.java | 108 ++++++++++++------ 2 files changed, 105 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java b/src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java index e7ea95d3f037..7676cc343653 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java @@ -1,11 +1,43 @@ package com.thealgorithms.datastructures.lists; /** - * Rotate a list - * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) + * The RotateSinglyLinkedLists class provides a method to rotate a singly linked list + * to the right by a specified number of positions. + *

+ * In a right rotation by `k` steps, each node in the list moves `k` positions to the right. + * Nodes that are rotated off the end of the list are placed back at the beginning. + *

+ *

+ * Example: + * Given linked list: 1 -> 2 -> 3 -> 4 -> 5 and k = 2, the output will be: + * 4 -> 5 -> 1 -> 2 -> 3. + *

+ *

+ * Edge Cases: + *

+ *

+ *

+ * Complexity: + *

+ *

+ * + * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) */ - public class RotateSinglyLinkedLists { + + /** + * Rotates a singly linked list to the right by `k` positions. + * + * @param head The head node of the singly linked list. + * @param k The number of positions to rotate the list to the right. + * @return The head of the rotated linked list. + */ public Node rotateRight(Node head, int k) { if (head == null || head.next == null || k == 0) { return head; diff --git a/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java index 8b2ae424364e..c5b70416e76e 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java @@ -6,67 +6,99 @@ import org.junit.jupiter.api.Test; /** - * Test cases for RotateSinglyLinkedLists + * Test cases for RotateSinglyLinkedLists. * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) */ public class RotateSinglyLinkedListsTest { + private final RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists(); + + // Helper method to create a linked list from an array of values + private Node createLinkedList(int[] values) { + if (values.length == 0) return null; + + Node head = new Node(values[0]); + Node current = head; + for (int i = 1; i < values.length; i++) { + current.next = new Node(values[i]); + current = current.next; + } + return head; + } + + // Helper method to convert a linked list to a string for easy comparison + private String linkedListToString(Node head) { + StringBuilder sb = new StringBuilder(); + Node current = head; + while (current != null) { + sb.append(current.value); + if (current.next != null) { + sb.append(" -> "); + } + current = current.next; + } + return sb.toString(); + } + @Test public void testRotateRightEmptyList() { - RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists(); - - // Test case: Rotate an empty list + // Rotate an empty list assertNull(rotator.rotateRight(null, 2)); } @Test public void testRotateRightSingleNodeList() { - RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists(); - - // Test case: Rotate a list with one element + // Rotate a list with a single element Node singleNode = new Node(5); Node rotatedSingleNode = rotator.rotateRight(singleNode, 3); - assertEquals(5, rotatedSingleNode.value); - assertNull(rotatedSingleNode.next); + assertEquals("5", linkedListToString(rotatedSingleNode)); } @Test public void testRotateRightMultipleElementsList() { - RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists(); + // Rotate a list with multiple elements (rotate by 2) + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5}); + Node rotated = rotator.rotateRight(head, 2); + assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated)); + } - // Test case: Rotate a list with multiple elements (Rotate by 2) - Node head = new Node(1); - head.next = new Node(2); - head.next.next = new Node(3); - head.next.next.next = new Node(4); - head.next.next.next.next = new Node(5); + @Test + public void testRotateRightFullRotation() { + // Rotate by more than the length of the list + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5}); + Node rotated = rotator.rotateRight(head, 7); + assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated)); + } - Node rotated1 = rotator.rotateRight(head, 2); - assertEquals(4, rotated1.value); - assertEquals(5, rotated1.next.value); - assertEquals(1, rotated1.next.next.value); - assertEquals(2, rotated1.next.next.next.value); - assertEquals(3, rotated1.next.next.next.next.value); - assertNull(rotated1.next.next.next.next.next); + @Test + public void testRotateRightZeroRotation() { + // Rotate a list by k = 0 (no rotation) + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5}); + Node rotated = rotator.rotateRight(head, 0); + assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated)); } @Test - public void testRotateRightFullRotation() { - RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists(); + public void testRotateRightByListLength() { + // Rotate a list by k equal to list length (no change) + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5}); + Node rotated = rotator.rotateRight(head, 5); + assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated)); + } - // Test case: Rotate a list with multiple elements (Full rotation) - Node head = new Node(1); - head.next = new Node(2); - head.next.next = new Node(3); - head.next.next.next = new Node(4); - head.next.next.next.next = new Node(5); + @Test + public void testRotateRightByMultipleOfListLength() { + // Rotate a list by a multiple of its length (no change) + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5}); + Node rotated = rotator.rotateRight(head, 10); // k = 2 * list length + assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated)); + } - Node rotated3 = rotator.rotateRight(head, 7); - assertEquals(4, rotated3.value); - assertEquals(5, rotated3.next.value); - assertEquals(1, rotated3.next.next.value); - assertEquals(2, rotated3.next.next.next.value); - assertEquals(3, rotated3.next.next.next.next.value); - assertNull(rotated3.next.next.next.next.next); + @Test + public void testRotateRightLongerList() { + // Rotate a longer list by a smaller k + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}); + Node rotated = rotator.rotateRight(head, 4); + assertEquals("6 -> 7 -> 8 -> 9 -> 1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated)); } } From 35fcb81c613fbf95d84908e7b384a95f16311fb2 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sat, 26 Oct 2024 03:40:21 +0000 Subject: [PATCH 2/4] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 98fbec625f5f..bd19d616be58 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -571,6 +571,7 @@ * [UnionFind](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UnionFind.java) * [UpperBound](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UpperBound.java) * slidingwindow + * [LongestSubstringWithoutRepeatingCharacters](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/LongestSubstringWithoutRepeatingCharacters.java) * [MaxSumKSizeSubarray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarray.java) * sorts * [AdaptiveMergeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/AdaptiveMergeSort.java) @@ -1163,6 +1164,7 @@ * [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java) * [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.java) * slidingwindow + * [LongestSubstringWithoutRepeatingCharactersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/LongestSubstringWithoutRepeatingCharactersTest.java) * [MaxSumKSizeSubarrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarrayTest.java) * sorts * [AdaptiveMergeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java) From 26d7ab2f92d0bea83f2f65e52f5e28f3700113c1 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sat, 26 Oct 2024 09:14:13 +0530 Subject: [PATCH 3/4] Fix --- .../datastructures/lists/RotateSinglyLinkedListsTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java index c5b70416e76e..3ba4842f8760 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java @@ -88,7 +88,6 @@ public void testRotateRightByListLength() { @Test public void testRotateRightByMultipleOfListLength() { - // Rotate a list by a multiple of its length (no change) Node head = createLinkedList(new int[] {1, 2, 3, 4, 5}); Node rotated = rotator.rotateRight(head, 10); // k = 2 * list length assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated)); From 36b8b98c495a379ac9a0a5c256f306953033b6cb Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sat, 26 Oct 2024 09:16:19 +0530 Subject: [PATCH 4/4] Fix --- .../datastructures/lists/RotateSinglyLinkedListsTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java index 3ba4842f8760..70c0dfccafa4 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java @@ -15,7 +15,9 @@ public class RotateSinglyLinkedListsTest { // Helper method to create a linked list from an array of values private Node createLinkedList(int[] values) { - if (values.length == 0) return null; + if (values.length == 0) { + return null; + } Node head = new Node(values[0]); Node current = head;