java如何递归复制列表?
这是我的代码:
public class ListItem {
final int number; //These need to stay this way because I need to access them later
ListItem next;
ListItem(int number, ListItem next) {
this.number = number;
this.next = next;
}
// I need to return a copy of this list using recursion, not iteration.
public ListItem duplicate() {
if (duplicate() == ListItem) { //base case??
return next;
}
else return duplicate(); //just a placeholder
}
我不确定基本情况应该是什么,递归调用应该是什么,因为duplicate()
没有参数。有人能告诉我Java是如何看待这些方法的吗?我想知道这是怎么回事
# 1 楼答案
或者更简洁地说: