Data Structures & AlgorithmsEasyHCL · Tech Mahindra pattern

What is the time complexity of inserting a new node at the head of a singly linked list of n nodes?

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

Correct answer: A. O(1)

Point the new node's next at the current head and make the new node the head: two pointer assignments, O(1). Inserting at the tail without a tail pointer is O(n).

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

Exams that ask this topic