I have a piece of code in Java that I have been struggling to understand what is the time complexity... In the best case scenario I believe it might be O(n) and the worst case probably O(n^2) because it could go in recursion for n times... Is this right??
All the other methods are O(1)
public void associateblocks() {
Block block = head;
while (block != null) {
Block block2 = block.getNextBlock();
if (block2 != null) {
if (block.getSize() == block2.getSize() && block.isFree() && block2.isFree()) {
block.setSize(block.getSize() * 2);
block.setIndex((block.getIndex() - 1) / 2);
block.setNextBlock(block2.getNextBlock());
associateblocks();
freeBlocks.remove(block2);
}
}
block = block.getNextBlock();
}
}