Skip to content

refactor: Enhance docs, add tests in ReverseKGroup #5999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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.
* </p>
* <p>
* Example:
* Given a linked list: 1 -> 2 -> 3 -> 4 -> 5 and k = 3,
* the output will be: 3 -> 2 -> 1 -> 4 -> 5.
* </p>
* <p>
* 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.
* </p>
* <p>
* Complexity:
* <ul>
* <li>Time Complexity: O(n), where n is the number of nodes in the linked list.</li>
* <li>Space Complexity: O(1), as we are reversing in place.</li>
* </ul>
* </p>
*
* 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;
Expand All @@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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)
Expand Down