From f8695404249566549f96f903520325e88125dbab Mon Sep 17 00:00:00 2001
From: Hardik Pawar
Date: Fri, 25 Oct 2024 18:29:34 +0530
Subject: [PATCH 1/2] refactor: Enhance docs, add tests in `ReverseKGroup`
---
.../datastructures/lists/ReverseKGroup.java | 57 +++++++++++++++++--
.../lists/ReverseKGroupTest.java | 5 +-
2 files changed, 53 insertions(+), 9 deletions(-)
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java b/src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java
index 3c4b9331266c..c9a5c1df9870 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java
@@ -1,11 +1,43 @@
package com.thealgorithms.datastructures.lists;
/**
- * Reverse K Group LinkedList (https://www.topcoder.com/thrive/articles/reverse-node-in-k-group)
+ * The ReverseKGroup class provides functionality to reverse nodes in a
+ * linked list in groups of k nodes.
+ *
+ * This algorithm follows the approach of reversing the linked list in segments of
+ * size k. If the remaining nodes are fewer than k, they remain unchanged.
+ *
+ *
+ * Example:
+ * Given a linked list: 1 -> 2 -> 3 -> 4 -> 5 and k = 3,
+ * the output will be: 3 -> 2 -> 1 -> 4 -> 5.
+ *
+ *
+ * The implementation contains:
+ * - {@code length(Node head)}: A method to calculate the length of the linked list.
+ * - {@code reverse(Node head, int count, int k)}: A helper method that reverses the nodes
+ * in the linked list in groups of k.
+ * - {@code reverseKGroup(Node head, int k)}: The main method that initiates the reversal
+ * process by calling the reverse method.
+ *
+ *
+ * Complexity:
+ *
+ * - Time Complexity: O(n), where n is the number of nodes in the linked list.
+ * - Space Complexity: O(1), as we are reversing in place.
+ *
+ *
+ *
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
*/
-
public class ReverseKGroup {
+
+ /**
+ * Calculates the length of the linked list.
+ *
+ * @param head The head node of the linked list.
+ * @return The total number of nodes in the linked list.
+ */
public int length(Node head) {
Node curr = head;
int count = 0;
@@ -15,7 +47,15 @@ public int length(Node head) {
}
return count;
}
- // reverse function
+
+ /**
+ * Reverses the linked list in groups of k nodes.
+ *
+ * @param head The head node of the linked list.
+ * @param count The remaining number of nodes.
+ * @param k The size of the group to reverse.
+ * @return The new head of the reversed linked list segment.
+ */
public Node reverse(Node head, int count, int k) {
if (count < k) {
return head;
@@ -37,9 +77,16 @@ public Node reverse(Node head, int count, int k) {
}
return prev;
}
+
+ /**
+ * Reverses the linked list in groups of k nodes.
+ *
+ * @param head The head node of the linked list.
+ * @param k The size of the group to reverse.
+ * @return The head of the modified linked list after reversal.
+ */
public Node reverseKGroup(Node head, int k) {
int count = length(head);
- Node ans = reverse(head, count, k);
- return ans;
+ return reverse(head, count, k);
}
}
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java b/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
index e7e3cca4083f..a1e9dabeaae3 100644
--- a/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
@@ -4,10 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
-/**
- * Test cases for Reverse K Group LinkedList
- * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
- */
+
public class ReverseKGroupTest {
@Test
From 351ff2f97c96186a3947030d937c268618bc7dc1 Mon Sep 17 00:00:00 2001
From: Hardik Pawar
Date: Tue, 29 Oct 2024 09:53:20 +0530
Subject: [PATCH 2/2] Fix
---
.../thealgorithms/datastructures/lists/ReverseKGroupTest.java | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java b/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
index a1e9dabeaae3..b2db478f692c 100644
--- a/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
@@ -5,6 +5,10 @@
import org.junit.jupiter.api.Test;
+/**
+ * Test cases for Reverse K Group LinkedList
+ * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
+ */
public class ReverseKGroupTest {
@Test