Suppose that we say:

Cons lst = null;
for (int i = 0; i < n; i++ )
    lst = append( list(i), lst );

What is the Big O of this code?

  • A: O(1)
  • B: O(n)
  • C: O(n log n)
  • D: O(n2)
  • E: error

    Answer: B

    The Big O of append is the size of its first argument, always 1 in this case. Since it is in a loop up to n, the total Big O is O(n * 1)   =   O(n).

    Contents    Page-10    Prev    Next    Page+10    Index