Data Structures & AlgorithmsEasyAccenture · Cognizant pattern

Only the head pointer of a singly linked list of n nodes is known (no length is stored). What is the time to reach its middle node?

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

Correct answer: B. O(n)

A linked list has no index arithmetic: you must walk node by node (or run slow/fast pointers), touching about n/2 nodes – O(n). O(1) access to the middle is an array property; binary search on a linked list is not O(log n) for the same reason.

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

Exams that ask this topic