ProgrammingModerate

A list reversal is done in place, and then the list is traversed to print it. What is the time for the reversal alone on n nodes?

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

Correct answer: B. O(n)

In-place reversal walks the list once with prev, curr and next pointers, re-pointing each link a single time: O(n) time and O(1) extra space. O(1) is the space bound, not the time.

More C Programming questions

Exams that ask this topic