g0001_0100.s0024_swap_nodes_in_pairs.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-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
**Time Complexity (Big O Time):**
1. The `getLength` function iterates through the linked list once to determine its length, which takes O(n) time, where n is the number of nodes in the list.
2. The `reverse` function is a recursive function that processes the linked list in pairs of 2 nodes. In each recursive call, it reverses two nodes, which takes constant time O(1). The number of recursive calls is proportional to the length of the list divided by 2 (len / 2).
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. This is because the dominant factor is the iteration through the list to compute its length.
**Space Complexity (Big O Space):**
1. The space complexity of the program is O(1) because it uses a constant amount of additional space for variables like `curr`, `prev`, and `next`. Regardless of the size of the input linked list, the space used for these variables remains constant.
2. The program does use a call stack for the recursive calls to the `reverse` function, but the maximum depth of the recursion is bounded by the length of the list divided by 2. Therefore, the space used for the call stack is O(n/2), which simplifies to O(n) in big O notation. However, this is still considered O(1) in terms of space complexity because it's not dependent on the input size.
In summary, the time complexity of the provided program is O(n), and the space complexity is O(1), where n is the number of nodes in the input linked list.
© 2015 - 2024 Weber Informatics LLC | Privacy Policy