user3913814
user3913814

Reputation: 17

Why this code gives big-O = O(1)

public static LinkedList third(int[] array){
    LinkedList retval = null;
    for (int i = 0; i < 999 && i < array.length; i = i + 1) {
        retval = new LinkedList(array[i], retval);
    }
    return retval;
}

Why this code gives big-O = O(1) ?

Upvotes: 1

Views: 80

Answers (1)

Tony Babarino
Tony Babarino

Reputation: 3405

Because the loop will be executed maximally 999 times which is a constant value therefore You can think of it as it's O(999) = O(1) = O(c), where c is a constant value.

If the value of i wouldn't be limited by 999, the loop would be executed array.length times and the complexity would be O(n), where n is the size of input array.

Upvotes: 2

Related Questions