scala – List – reverse

like all other list operations, reverse creates a new list rather than changing the one it operates on. Lists are immutable

Reverse could be implemented using concatenation(:::),like in the following method, rev:
def rev[T](xs: List[T]): List[T] = xs match

{

case List() => xs

case x :: xs1 => rev(xs1) ::: List(x)

}

To study the complexity of rev, assume that the list xs has length n. Notice that there are n recursive calls to rev. Each call except the last involves a list concatenation. List concatenation xs ::: ys takes time proportional to the length of its first argument xs. Hence, the total complexity of rev is: n+(n−1)+…+1 = (1+n)∗n/2

rev’s complexity is quadratic in the length of its input argument. This is disappointing when compared to the standard reversal of a mutable, linked list, which has linear complexity

Leave a comment