DEV Community

loading...

DSA

Devansh Tayal
Everything under the sun is my syllabus. And I'm loving that i follow my syllabus being efficient.
・1 min read

What is the worst-case time complexity of the following algorithm to delete the node from the list?

// Find next node using next pointer
struct node *temp = node_ptr->next;
// Copy data of next node to this node
node_ptr->data = temp->data;
// Unlink next node
node_ptr->next = temp->next;
// Delete next node
free(temp);

a) O(n)
b) O(log2 n)
c) O(logn)
d) O(1)

(d)
Explanation: It is a Fast solution to delete a particular node from the linked list is to copy the data from the next node to the node to be deleted and delete the next node.Time complexity of this approach is O(1).

Discussion (0)