I wrote an algorithm to find duplicates in a linked-list.
for each node, I iterate from head of the list to the current node, and if it’s a duplicate it is deleted.
what is the complexity of my algorithm?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The complexity of the algorithm is
Θ(n^2)worst case, since if there are no dupes, you iterate for each node a linearly increasing number of times, resulting in total of1 + 2 + .... + ntotal reads which isΘ(n^2)(from sum of arithmetic progression)At best case the complexity is
Θ(n)– if all elements are dupes The complexity isΘ(n), because at every iteration the list shrinks, which results in at most 2 node reads per iteration, thusΘ(n)