What is the worst-case time complexity of inserting an element into a singly linked list at the head position?

Answer and explanation

Correct answer: A. O(1)

  1. Step 1: Trace head insertion in a linked list: create a new node, set `new_node.next = head`, then update `head = new_node`.
  2. Step 2: The operation requires updating two pointer references without traversing any other nodes in the list, executing in constant time .
Answer
O(1).
Why not the others: is required to insert at the tail without a tail pointer or at an arbitrary index; applies to trees.

Practise more Algorithms and Python Programming.

Free questions with an explanation for every one, and full timed EmSAT Achieve (UAE) mock tests.