1

I was looking at question Link and it tells that space complexity of solution is O(1) (read answer by Max). I have doubt that space complexity is the space which is needed by the algorithm and I had understood that correctly and feels that it is definitely O(n) where n is size of linked list. Can anyone tell that is that answer wrong or I have made mistake in understanding?

1 Answer 1

1

The summary of the answer in Max's link, here, is clearly mistaken. O(1) space complexity is impossible by definition, if the goal is to copy some variable amount of data (in this case linked list).

This is seen in the algorithm's description:

Create the copy of node 1 and insert it between node 1 & node 2 in original Linked List, create the copy of 2 and insert it between 2 & 3.. Continue in this fashion, add the copy of N afte the Nth node Blockquote

Here, the answerer has just added "N" nodes, so it's at least O(n) complexity (and, indeed, the space complexity of the algorithm listed is O(n)).

Sign up to request clarification or add additional context in comments.

4 Comments

Can you give me the link where the space complexity of algorithm is listed? And why did so many people upvoted his answer without thinking this?
Hi Parth Patel, the link is provided in the original question (albeit indirectly, as you have to look at Max's answer). This topic is basically asking "is Max's answer correct; and why", which means that answers such as mine are within the context Max's answer, which already contains the link. Nonetheless I have now provided it here also.
Ok thanks.The link you have given is correct and in that,it's clearly written that auxiliary space is O(1) which is true but not space complexity which is different from auxiliary space.
Yes. Everything about it is correct except for the summary of the space complexity right at the end. It's possibly just a typo by the author. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.