0 0 votes Please explain how to approach this problem? Mathematical Logic linked-list + – Parshu gate 840 views answer comment Share Follow Print See 1 comment 1 1 comment reply srivivek95 commented Nov 19, 2017 reply Follow flag If the linked list is not circular then we can insert the element in the beginning by just : link.new=start; start=new; This is O(1) operation. But as the linked list is circular, in addition to the above operation we also need to search for the last element {$\Theta$(n) operation} & then update: link.new=start; start=new; // reach to last node in $\Theta$(n) time link.last=start; 1 1 replyShare Please log in or register to add a comment.
1 1 vote Thus inserting when only 1 node is present is best case while when there are n nodes then to connect last pointer to start of new node we need time O(n) hence average case is $\Theta(n)$ kirti_k answered Nov 20, 2017 kirti_k comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote If you have pointer to the first node Insertion at beginning: O(n) Insertion at the end: O(n) // As you have to traverse till the end. If you have pointer to the last node Insertion at beginning: O(1) Insertion at end: O(1) In GATE question they will most likely clearly mention to which nodes we have the pointer to. Check this: https://gateoverflow.in/1033/gate2004-36 smsubham answered Feb 12, 2018 • edited Apr 30, 2018 by smsubham smsubham comment Share Follow 0 reply Please log in or register to add a comment.