/** Returns an IntList identical to L, but with
* each element incremented by x. L is not allowed
* to change. */
public static IntList incrList(IntList L, int x) {
if (L == null ){
return null;//
}else {
return new IntList(L.first + x, incrList(L.rest, x));//recursion, 把L.rest重复copy到L
}
}
/** Returns an IntList identical to L, but with
* each element incremented by x. Not allowed to use
* the 'new' keyword. */
public static IntList dincrList(IntList L, int x) {
if (L == null){
return null;
}
L.first += x;
L.rest = dincrList(L.rest, x);
//recursion 需要破坏L所以不能用new直接返回新的列表,需要改变L中的item之后再返回L//
return L;
}