Contents    Page-10    Prev    Next    Page+10    Index   

Copying a List

Since reverse is constructive, we could copy a list by reversing it twice:

public static Cons copy_list (Cons lst) {
    return reverse(reverse(lst)); }

What is the Big O of this function?

We could criticize the efficiency of this function because it creates O(n) garbage: the list produced by the first reverse is unused and becomes garbage when the function exits. However, the Big O of the function is still O(n) + O(n) = O(n).