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 ?
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 ?
Ask the lecturer, or choose O(1) and send an email to him after exam. – nhahtdh Mar 23 '13 at 10:26tail
and to thehead
, you can insert in front ofhead
by letting the tail point to the new element and the new element point tohead
. Then you let the new element be the newhead
. If you do not keep a pointer to thetail
element, then you are not able to do this, but I cannot see why you would not sacrifice a one bit memory allocation for a linear time improvement. What does the book say on this? – Pål GD Mar 23 '13 at 12:57But even with only one pointer it can be achieved. You insert a new node next to head, copy the value of earlier node which is pointed by head to this new node created & then you insert a value to the node pointed by head.
– avi Mar 23 '13 at 14:03http://www.youtube.com/watch?feature=player_detailpage&v=PGWZUgzDMYI#t=1922s
– avi Mar 23 '13 at 14:04And also agree about MCQs. Have you heard of GATE exam ? It is a post graduate entrance exam in India. The questions usually ask for complexity of a algorithm & they use a Big-Oh in questions. Let's say for example, if they ask question, what is the worst-case complexity of Quick Sort. The options will be having $O(n^2lgn)$, $O(n^2)$ and $O(n^3)$. They use Big-Oh notation only & if you chose any option other than $O(n^2)$, it will be considered as wrong & $.30$ marks will be deducted.
– avi Mar 23 '13 at 16:37