g0001_0100.s0025_reverse_nodes_in_k_group.complexity.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
**Time Complexity (Big O Time):**
1. The program first checks if the length of the linked list is less than k by iterating through the list. This operation takes O(k) time.
2. The main logic of reversing the linked list is contained in a loop that iterates k times (for each group of k nodes). Inside this loop, the reversal of the k nodes takes constant time, O(k). This is because regardless of the size of the input linked list, you are reversing a fixed number of nodes (k) in each iteration.
3. The program uses recursion to reverse the remaining part of the linked list, which will also be done in groups of k nodes. The depth of the recursion will depend on the length of the input linked list, but each level of recursion deals with a sublist of the original list, which is a fraction of the size. Therefore, the overall time complexity of the recursion can be expressed as O(n), where n is the number of nodes in the input linked list.
Combining these factors, the overall time complexity of the program is O(n), where n is the number of nodes in the input linked list. The O(k) part is overshadowed by the O(n) complexity.
**Space Complexity (Big O Space):**
1. The space complexity of the program is O(k), where k is the group size. This is because in each iteration of the reversal loop, a constant amount of additional space is used for variables like `c`, `n`, `prev`, and `i`. The recursion also consumes space on the call stack, but the maximum depth of the recursion is bounded by the length of the list divided by k, so the space used for the call stack can be considered O(n/k), which simplifies to O(n) when analyzing in big O notation.
In summary, the time complexity of the provided program is O(n), and the space complexity is O(k) for the reversal logic within each group of k nodes, and O(n) for the space used on the call stack during recursion, where n is the number of nodes in the input linked list and k is the group size.