Data Structures & AlgorithmsEasyAccenture · Cognizant pattern

In a doubly linked list you are given a pointer to a node X somewhere in the middle. What is the time to delete X?

  1. A. O(n)
  2. B. O(1)
  3. C. O(log n)
  4. D. O(n²)

Correct answer: B. O(1)

X knows both neighbours through its prev and next pointers, so deletion is X.prev.next = X.next and X.next.prev = X.prev – constant work. In a singly linked list you would need the predecessor, which costs O(n) to find from the head.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic