Data Structures · Question 17 · Easy

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

  1. O(n²)
  2. O(1)
  3. O(log n)
  4. O(n)
Answer and explanation

Answer: B. O(1)

Explanation: The new node's next pointer is set to the current head and head is updated to the new node. No traversal is needed, so it takes constant time.

Previous: Which data structure is the most natural choice for implementing the Undo… · Next: A data structure allows insertion and deletion at both the front and the rear…

Practise data structures

Try a fresh set of data structures questions with an explanation after every answer, free and without signing up: Data Structures practice. Or sit a full placement mock test.

More data structures questions

More technical & programming practice

Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks · Software Engineering · Cloud & DevOps

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge