From 536978919dc79af9bf25c57ebb90802d1830784e Mon Sep 17 00:00:00 2001 From: Bama Charan Chhandogi Date: Tue, 3 Oct 2023 12:13:49 +0530 Subject: [PATCH] Add reverse k group in LinkedList algorithm (#4532) --- .../datastructures/lists/ReverseKGroup.java | 45 ++++++++++++ .../lists/ReverseKGroupTest.java | 72 +++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/lists/ReverseKGroup.java create mode 100644 src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java 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; + } +} 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); + } +}