Skip to content

Commit d68d840

Browse files
authored
Merge branch 'master' into master
2 parents 935aea1 + 5369789 commit d68d840

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
/**
4+
* Reverse K Group LinkedList (https://www.topcoder.com/thrive/articles/reverse-node-in-k-group)
5+
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
6+
*/
7+
8+
public class ReverseKGroup {
9+
public int length(Node head) {
10+
Node curr = head;
11+
int count = 0;
12+
while (curr != null) {
13+
curr = curr.next;
14+
count++;
15+
}
16+
return count;
17+
}
18+
// reverse function
19+
public Node reverse(Node head, int count, int k) {
20+
if (count < k) {
21+
return head;
22+
}
23+
Node prev = null;
24+
int count1 = 0;
25+
Node curr = head;
26+
Node Next = null;
27+
while (curr != null && count1 < k) {
28+
Next = curr.next;
29+
curr.next = prev;
30+
prev = curr;
31+
curr = Next;
32+
count1++;
33+
}
34+
35+
if (Next != null) {
36+
head.next = reverse(Next, count - k, k);
37+
}
38+
return prev;
39+
}
40+
public Node reverseKGroup(Node head, int k) {
41+
int count = length(head);
42+
Node ans = reverse(head, count, k);
43+
return ans;
44+
}
45+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
/**
4+
* Test cases for Reverse K Group LinkedList
5+
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
6+
*/
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
public class ReverseKGroupTest {
13+
14+
@Test
15+
public void testReverseKGroupWithEmptyList() {
16+
ReverseKGroup reverser = new ReverseKGroup();
17+
assertNull(reverser.reverseKGroup(null, 3));
18+
}
19+
20+
@Test
21+
public void testReverseKGroupWithSingleNodeList() {
22+
ReverseKGroup reverser = new ReverseKGroup();
23+
Node singleNode = new Node(5);
24+
Node result = reverser.reverseKGroup(singleNode, 2);
25+
assertEquals(5, result.value);
26+
assertNull(result.next);
27+
}
28+
29+
@Test
30+
public void testReverseKGroupWithKEqualTo2() {
31+
ReverseKGroup reverser = new ReverseKGroup();
32+
33+
// Create a list with multiple elements (1 -> 2 -> 3 -> 4 -> 5)
34+
Node head;
35+
head = new Node(1);
36+
head.next = new Node(2);
37+
head.next.next = new Node(3);
38+
head.next.next.next = new Node(4);
39+
head.next.next.next.next = new Node(5);
40+
41+
// Test reverse with k=2
42+
Node result1 = reverser.reverseKGroup(head, 2);
43+
assertEquals(2, result1.value);
44+
assertEquals(1, result1.next.value);
45+
assertEquals(4, result1.next.next.value);
46+
assertEquals(3, result1.next.next.next.value);
47+
assertEquals(5, result1.next.next.next.next.value);
48+
assertNull(result1.next.next.next.next.next);
49+
}
50+
51+
@Test
52+
public void testReverseKGroupWithKEqualTo3() {
53+
ReverseKGroup reverser = new ReverseKGroup();
54+
55+
// Create a list with multiple elements (1 -> 2 -> 3 -> 4 -> 5)
56+
Node head;
57+
head = new Node(1);
58+
head.next = new Node(2);
59+
head.next.next = new Node(3);
60+
head.next.next.next = new Node(4);
61+
head.next.next.next.next = new Node(5);
62+
63+
// Test reverse with k=3
64+
Node result = reverser.reverseKGroup(head, 3);
65+
assertEquals(3, result.value);
66+
assertEquals(2, result.next.value);
67+
assertEquals(1, result.next.next.value);
68+
assertEquals(4, result.next.next.next.value);
69+
assertEquals(5, result.next.next.next.next.value);
70+
assertNull(result.next.next.next.next.next);
71+
}
72+
}

0 commit comments

Comments
 (0)