Repository: TheAlgorithms/Java
Pull request #4345 (2023-09-01 20:10:53)

add Rotate a Linkedlist
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


BamaCharanChhandogi: Hello, @siriak  check, please!

Patch:
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java b/src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java
new file mode 100644
index 00000000..e7ea95d3
--- /dev/null
+++ b/src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java
@@ -0,0 +1,33 @@
+package com.thealgorithms.datastructures.lists;
+
+/**
+ * Rotate a list
+ * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
+ */
+
+public class RotateSinglyLinkedLists {
+    public Node rotateRight(Node head, int k) {
+        if (head == null || head.next == null || k == 0) {
+            return head;
+        }
+
+        Node curr = head;
+        int len = 1;
+        while (curr.next != null) {
+            curr = curr.next;
+            len++;
+        }
+
+        curr.next = head;
+        k = k % len;
+        k = len - k;
+        while (k > 0) {
+            curr = curr.next;
+            k--;
+        }
+
+        head = curr.next;
+        curr.next = null;
+        return head;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java
new file mode 100644
index 00000000..23780758
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java
@@ -0,0 +1,72 @@
+package com.thealgorithms.datastructures.lists;
+
+/**
+ * Test cases for RotateSinglyLinkedLists
+ * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
+ */
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+public class RotateSinglyLinkedListsTest {
+
+    @Test
+    public void testRotateRightEmptyList() {
+        RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
+
+        // Test case: 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
+        Node singleNode = new Node(5);
+        Node rotatedSingleNode = rotator.rotateRight(singleNode, 3);
+        assertEquals(5, rotatedSingleNode.value);
+        assertNull(rotatedSingleNode.next);
+    }
+
+    @Test
+    public void testRotateRightMultipleElementsList() {
+        RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
+
+        // 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);
+
+        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 testRotateRightFullRotation() {
+        RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
+
+        // 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);
+
+        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);
+    }
+}