Complexity of algorithm inserting an element in a circular linked list at the front end
-
16-10-2019 - |
Question
In a circular linked list, if an elements needs to be inserted at front [just before the node pointed by head], can be done in O(1) (see the answer here)
But in a book currently, I have, it is mentioned that it is done in O(n) (the usual method). I also saw few lecture ppts, they all mention the usual method of traversing the list & adding an element.
My question is :
In practical scenarios which method is used ?
I am about to attend an exam, which consists of MCQs, if above question is asked shall I mark O(n), since that is the standard answer ?
Solution
The method used in practical scenarios depends on the scenario (and on the programmer). There are several possible issues influencing the choice of implementation:
- Whether the algorithm is known to the programmer.
- Ease of coding (it's easiest if it's already implemented in some library you can use).
- Speed - that depends on how the data structure is used.
- Space overhead taken by the data structure.
An intelligent programmer should take all of these into account, and try to make themselves more knowledgeable of various algorithms and data structures.