我有这个问题:
编写一个递归java函数,循环计算循环中的节点数 链接列表。
这是方法/函数:
public int countNodes(Node node){
if (node == null) {
return 0;
}
else{
return 1+countNodes(node.next);
}
}
我在该行中遇到错误 返回 1+countNodes(node.next); 这表示:next 无法解析为 java 中的值或字段
我应该做什么来修复这个错误?
最佳答案
Node 类应该是这样的:
public class Node {
private Node next;
public Node getNext(){
return next;
}
//setter and other stuff
}
并且你必须调用node.getNext()
return 1+countNodes(node.getNext());
不要忘记 Java 是一种面向对象的语言。
关于java - next 无法解析为 java 中的值或字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23302780/