Time Complexity | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
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 Antwort
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