Mandy
Mandy

Reputation: 145

Big-O notation of piece of code that initializes an array and linked list

I am trying to develop more understanding about running times.

Suppose I have the code inside my function, and each statement has some different time complexities:

LinkedList myLL = new LinkedList(); //O(1)

myLL.addAtHead("1"); //O(1)
myLL.addAtHead("2"); //O(1)
myLL.addAtHead("3"); //O(1)

int[] myArray = new int[n] //O(n) , depending on what n is
<Some other statement> //O(n^2)

Determining the running time, it is O(n^2) ? Do we just consider statement that takes the most time and say that the total running time is O(n^2)?

Upvotes: 0

Views: 24

Answers (1)

cnst
cnst

Reputation: 27238

Yes, the prevailing term cancels all the other ones (well, it makes them insignificant in the overall consideration), so, the complexity is O(n^2).

Upvotes: 1

Related Questions