【发布时间】:2020-10-12 21:59:35
【问题描述】:
我正在尝试编写一个函数incrList(L, x),它递归地复制给定的线性链表L,并以一个常数值x 递增。当我在终端中编译时,我得到了IntList@6ff3c5b5,这是一个内存位置,而不是实际的列表。我只想更改函数 incrList 本身以提供正确的输出。
public class IntList {
public int first;
public IntList rest;
public IntList(int f, IntList r) {
first = f;
rest = r;
}
}
public class Lists1Exercises{
public static IntList incrList(IntList L, int x) {
if (L == null){
return null;
}else {
IntList head = new IntList(L.first+x, null);
head.rest = incrList(L.rest, x);
return head;
}
}
public static void main(String[] args) {
IntList L = new IntList(5, null);
L.rest = new IntList(7, null);
L.rest.rest = new IntList(9, null);
System.out.println(incrList(L, 3));
}
}
【问题讨论】:
标签: java