/** Prepend given list of elements to front of list, forming and returning a new list. */ public List<A> prependList(List<A> xs) { if (this.isEmpty()) return xs; if (xs.isEmpty()) return this; if (xs.tail.isEmpty()) return prepend(xs.head); // return this.prependList(xs.tail).prepend(xs.head); List<A> result = this; List<A> rev = xs.reverse(); Assert.check(rev != xs); // since xs.reverse() returned a new list, we can reuse the // individual List objects, instead of allocating new ones. while (rev.nonEmpty()) { List<A> h = rev; rev = rev.tail; h.setTail(result); result = h; } return result; }