Repository: TheAlgorithms/Java
Pull request #4532 (2023-10-01 22:09:55)

add reverse k group LinkedList
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- 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: @vie02 check!

Patch:
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java b/src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java
new file mode 100644
index 00000000..bd599e2a
--- /dev/null
+++ b/src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java
@@ -0,0 +1,45 @@
+package com.thealgorithms.datastructures.lists;
+
+/**
+ * Reverse K Group LinkedList (https://www.topcoder.com/thrive/articles/reverse-node-in-k-group)
+ * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
+ */
+
+public class ReverseKGroup {
+    public int length(Node head) {
+        Node curr = head;
+        int count = 0;
+        while (curr != null) {
+            curr = curr.next;
+            count++;
+        }
+        return count;
+    }
+    // reverse function
+    public Node reverse(Node head, int count, int k) {
+        if (count < k) {
+            return head;
+        }
+        Node prev = null;
+        int count1 = 0;
+        Node curr = head;
+        Node Next = null;
+        while (curr != null && count1 < k) {
+            Next = curr.next;
+            curr.next = prev;
+            prev = curr;
+            curr = Next;
+            count1++;
+        }
+
+        if (Next != null) {
+            head.next = reverse(Next, count - k, k);
+        }
+        return prev;
+    }
+    public Node reverseKGroup(Node head, int k) {
+        int count = length(head);
+        Node ans = reverse(head, count, k);
+        return ans;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java b/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
new file mode 100644
index 00000000..8273b890
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
@@ -0,0 +1,72 @@
+package com.thealgorithms.datastructures.lists;
+
+/**
+ * Test cases for Reverse K Group LinkedList
+ * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
+ */
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+public class ReverseKGroupTest {
+
+    @Test
+    public void testReverseKGroupWithEmptyList() {
+        ReverseKGroup reverser = new ReverseKGroup();
+        assertNull(reverser.reverseKGroup(null, 3));
+    }
+
+    @Test
+    public void testReverseKGroupWithSingleNodeList() {
+        ReverseKGroup reverser = new ReverseKGroup();
+        Node singleNode = new Node(5);
+        Node result = reverser.reverseKGroup(singleNode, 2);
+        assertEquals(5, result.value);
+        assertNull(result.next);
+    }
+
+    @Test
+    public void testReverseKGroupWithKEqualTo2() {
+        ReverseKGroup reverser = new ReverseKGroup();
+
+        // Create a list with multiple elements (1 -> 2 -> 3 -> 4 -> 5)
+        Node head;
+        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 reverse with k=2
+        Node result1 = reverser.reverseKGroup(head, 2);
+        assertEquals(2, result1.value);
+        assertEquals(1, result1.next.value);
+        assertEquals(4, result1.next.next.value);
+        assertEquals(3, result1.next.next.next.value);
+        assertEquals(5, result1.next.next.next.next.value);
+        assertNull(result1.next.next.next.next.next);
+    }
+
+    @Test
+    public void testReverseKGroupWithKEqualTo3() {
+        ReverseKGroup reverser = new ReverseKGroup();
+
+        // Create a list with multiple elements (1 -> 2 -> 3 -> 4 -> 5)
+        Node head;
+        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 reverse with k=3
+        Node result = reverser.reverseKGroup(head, 3);
+        assertEquals(3, result.value);
+        assertEquals(2, result.next.value);
+        assertEquals(1, result.next.next.value);
+        assertEquals(4, result.next.next.next.value);
+        assertEquals(5, result.next.next.next.next.value);
+        assertNull(result.next.next.next.next.next);
+    }
+}