1) The time cost to add n elements to an initially empty singly linked list by
inserting at the front of the list.
the answer seems to be one of these O(n) or O(1).
I think it is O(1) because
inserting an element into empty list is just
for example Node element = 1;
But I’m still not sure about this.
2)What would be the best-case time cost to find a data element in a linked list with n elements.
The answer also seems to be either O(1) or O(n).
I think it’s O(n) because it has to traverse through the list to find the element.
It is
O(1)per insertion, but you havenof those – soO(n)at total.It is
O(1), because at best case – the searched element is the first one, so there is no need to traverse the list, after searching the first element (which is constant time) – you can halt.