Data Structures & AlgorithmsModerateProduct-company MCQ pattern

In a singly linked list you are given only a pointer to a node X (not the last node) and no head pointer. How can X be deleted, and in what time?

  1. A. Copy the next node's data into X and unlink the next node: O(1)
  2. B. It cannot be deleted without the head pointer
  3. C. Traverse from the head to X's predecessor: O(n)
  4. D. Swap X with the tail node: O(n)

Correct answer: A. Copy the next node's data into X and unlink the next node: O(1)

X takes over its successor's data and next pointer, and the successor is freed, which is O(1). The trick fails only for the last node, which is why the question excludes it.

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

Exams that ask this topic