Sobes.tech
Junior — Middle

How to estimate the time costs when adding an element to the beginning of a singly linked list?

sobes.tech AI

Answer from AI

Adding an element to the beginning of a singly linked list is an operation with a time complexity of O(1). This is because, to insert a new node at the beginning of the list, it is sufficient to:

  1. Create a new node.
  2. Point its link to the current first element of the list.
  3. Update the head pointer of the list to the new node.

Since there is no need to traverse the list, the insertion time does not depend on the size of the list.

How to estimate the time costs when adding an element… - sobes.tech