• edited by
41,658 views
85 85 votes

Let $P$ be a singly linked list. Let $Q$ be the pointer to an intermediate node $x$ in the list. What is the worst-case time complexity of the best-known algorithm to delete the node $x$ from the list ?

  1. $O(n)$
  2. $O(\log^2 n)$
  3. $O(\log n)$
  4. $O(1)$

9 Answers

Best answer
79 79 votes

In the worst case $x$ could be last or second last node, In that case full traversal of the list is required. Therefore answer is (A).

Procedure for deleting node that contains $x$ (pointed by $Q$):

  1. Identify the node whose next is pointing to $x$, note it as $' \text{prev}_x' \Rightarrow \text{This takes O(n)}$
  2. Identify the node which is pointed by $Q.next$, note it as $' \text{next}_x' \Rightarrow \text{This takes O(1)}$
  3. $\text{prev}_x -> next = \text{next}_x$
  4. free(Q)


PS: We can simulate the deletion by moving the $x's$ next node data to $x$ and then delete $x's$ next node. But this is technically not the same as DELETING NODE $x$ as mentioned in the question though effect is the same as long as there are a constant number of elements to be moved from $x's$ next node.

• edited by
95 95 votes
Since $Q$ is pointing to node $X$, it can de done in $O(1)$ time..

Algo:

$Q \rightarrow data = Q \rightarrow next \rightarrow data$; // Copy the value of next node into $Q$.

$del = Q \rightarrow  next$; // take another pointer variable pointing to next node of $Q$.

$Q \rightarrow next = Q \rightarrow next \rightarrow next$;

$free (del)$;

Correct Answer: $D$
• edited by
12 12 votes
Its O(1) only. Just copy next element's data to x ,and delete next.
10 10 votes
Let us say assume ,question it means node with data X: Now we first need to search node with data X in O(n) and then delete.

Let us assume that by X ,it means Node with address X,but the 0(1) method does not delete node with address X.It just modifies node with address X and delete some of its neighbor.

I will go with $ O(n) $ with the above reasoning.
10 10 votes

Answer is A , O(n) for sure. 
I feel the person who has given answer as O(1) has thought too much and have changed the meaning of the question, the question asks for deleting the Node X ,  which is pointed by the pointer , they never asked anything about the data , the O(1) algorithm is manipulating the data but that node still anyway exist.  
if the question was about deleting the data/element then of course it would be O(1) but here the question is different. 

1 flag:
✌ Edit necessary (Aman Koli “WRONG ANSWER”)
6 6 votes
The answer is O(1).

Although worst case time complexity is asked but we have been given that Q is an internal node so it cannot be the first or last node and thus we can simply copy data.

struct node* t=q;

q->data=q->next->data;

q->next=q->next->next;

free(t);

these operations could be done in O(1) time.

Please let me know in the comments if there is something wrong with my answer.

Thanks forasking this question!
1 flag:
✌ Edit necessary (Aman Koli “WRONG ANSWER”)
Answer:
Position:
Show:

Related questions

69 69 votes
5 answers 5 answers
18.2k
18.2k views
Ishrat Jahan asked Nov 2, 2014
18,162 views
An array of integers of size $n$ can be converted into a heap by adjusting the heaps rooted at each internal node of the complete binary tree starting at the node $\left ...
31 31 votes
3 answers 3 answers
10.0k
10.0k views
Ishrat Jahan asked Nov 2, 2014
9,951 views
A program attempts to generate as many permutations as possible of the string, '$abcd$' by pushing the characters $a, b, c, d$ in the same order onto a stack, but it may ...
31 31 votes
5 answers 5 answers
8.1k
8.1k views
Ishrat Jahan asked Nov 2, 2014
8,098 views
Which one of the following binary trees has its inorder and preorder traversals as $BCAD$ and $ABCD$, respectively?
3 3 votes
1 answers 1 answer
3.9k
3.9k views
Ishrat Jahan asked Nov 2, 2014
3,931 views
Given below are several usages of the anchor tag in HTML.<A HREF = "http://www.gate.ac.in/HTML/BASIC/testpage.html">Test Me</A><A HREF = "/BASIC/testpage.html">Test Me</A...