The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+4 votes
31 views

What would be the asymptotic time complexity to add a node at the end of singly linked list, if the pointer is initially pointing to the head of the list?

  1. O(n)
  2. O(1)
  3. θ(n2)
  4. θ(n)
asked in Programming by (495 points) | 31 views

both O(n) and θ(n) are correct.

Okay! i don't know why, but the answer key go with θ(n).

But i also clearly agree with both the options.
I think in general it should be O(n) as best case is O(1) answorst is O(n). But the question specifies that we have to add a node at last when the pointer is initially at start. So in best case and worst case both it comes O(n) that's why the answer is Theta(n).
in any case, you need to reach the last node to insert new node  so it cant be done by traversing  less than n nodes.so theta(n) is correct

1 Answer

0 votes
Answer would be 4- theta(n)

However o(n) is also possible but most appropriate will be theta(n)  because here on both worst and best case we have to traverse at the end of list to add the new node..
answered ago by Active (2k points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

29,167 questions
36,992 answers
92,225 comments
34,837 users