Data Structures & AlgorithmsModerateGATE CS · DSA

In a singly linked list with only a head pointer, which operation CANNOT be done in O(1) time?

  1. A. Inserting after a node to which you hold a pointer
  2. B. Deleting the first node
  3. C. Inserting a new first node
  4. D. Deleting the last node

Correct answer: D. Deleting the last node

Deleting the tail requires the second-last node so its next pointer can be set to NULL, which takes a Θ(n) walk. The other three only touch the head or a known node.

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

Exams that ask this topic