+1 vote
in Data Structures & Algorithms I by (110k points)
What would be the asymptotic time complexity to insert an element at the second position in the linked list?

(a) O(1)

(b) O(n)

(c) O(n^2)

(d) O(n^3)

My question is based upon Singly Linked List Operations topic in division Abstract Data Types of Data Structures & Algorithms I

This question was addressed to me in class test.

1 Answer

+1 vote
by (550k points)
selected by
Best answer
Right option is (a) O(1)

For explanation: A new node is created with the required element. The pointer of the new node points the node to which the head node of the linked list is also pointing. The head node pointer is changed and it points to the new node which we created earlier. The entire process completes in O (1) time. Thus the asymptotic time complexity to insert an element in the second position of the linked list is O (1).

Related questions

Welcome to TalkJarvis QnA, a question-answer community website for the people by the people. On TalkJarvis QnA you can ask your doubts, curiosity, questions and whatever going in your mind either related to studies or others. Experts and people from different fields will answer.