From 09950d6097ea7b2590fb80336d144ee402e61226 Mon Sep 17 00:00:00 2001 From: Bama Charan Chhandogi Date: Sun, 3 Sep 2023 01:02:28 +0530 Subject: [PATCH] Add Rotate a Linkedlist (#4345) --- .../lists/RotateSinglyLinkedLists.java | 33 +++++++++ .../lists/RotateSinglyLinkedListsTest.java | 72 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java create mode 100644 src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java 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 000000000..e7ea95d3f --- /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; + } +} 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 000000000..23780758b --- /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); + } +}