Time Complexity | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Time Complexity

I get confused when it comes to this, but would the time complexity for this method be O(1)? public void push(T target) { //TODO // throw new UnsupportedOperationException("Not supported yet.") SinglyLinkedNode<T> temp = new SinglyLinkedNode<T>(target); temp.setNext(head); head = temp; size++; }

13th Mar 2020, 7:41 PM
Cyrus
1 Answer
0
Yes. Creating a new node is O(1), .setNext is O(1), assigning temp to head is O(1). You are not looping or recursing anywhere here so the code is O(1).
14th Mar 2020, 12:00 PM
Schindlabua
Schindlabua - avatar