What is the time complexity to count the number of elements in the linked list ?
2024
What is the time complexity to count the number of elements in the linked list ?
- A.
O(1)
- B.
O(n)
- C.
O(logn)
- D.
O(n²)
Attempted by 238 students.
Show answer & explanation
Correct answer: B
Counting elements in a linked list requires traversing all nodes from head to tail. This linear traversal means time complexity is O(n) where n is the number of nodes.