Suppose that we say:

LinkedList<Integer> lst = ...;
int sum = 0;
for (Integer i : lst )
    sum += i;

What is the Big O of this code?

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

    Answer: B

    Using the for-each loop with the : is convenient, involves less code, and has a good Big O for both LinkedList and ArrayList.

    Contents    Page-10    Prev    Next    Page+10    Index