in DS edited by
6,109 views
1 vote
1 vote

What is the time required to insert an element in a stack with linked implementation ?

  1. $O (\log_2 n)$
  2. $O (n)$
  3. $O (n\log_2 n)$
  4. $O (1)$
in DS edited by
6.1k views

2 Comments

here nothing is given about the order in which the element to be inserted , we just have to take the data from the list and push it onto the stack...since both operations requires constant time O(1) would be the correct answer...

Note:if n elements are to be inserted then complexity would be O(n).
1
1
O(1)
1
1

2 Answers

1 vote
1 vote
D is the answer

Since the operation will involve only a pointer modification which will take O(1)

1 comment

o(1) will be answer as element that is to be inserted in front of  top pointer  and then top pointer is modified to point to that new inserted element
0
0
0 votes
0 votes
Insertion/PUSH is equivalent to the insertion of a node at the beginning of the linked list, which requires constant time. Just modification of two pointers. Therefore PUSH requires constant time.

Hence option D.