Add reverse k group in LinkedList algorithm (#4532)

This commit is contained in:
Bama Charan Chhandogi 2023-10-03 12:13:49 +05:30 committed by GitHub
parent 6283572637
commit 536978919d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 117 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}