java - 编写一个递归方法来计算链接节点链中的节点数

标签 java recursion linked-list

我尝试了很多编码来解决以下问题,但也找不到答案。谁能帮我解决我的问题并告诉我哪里编码错误?

/** Task: Recusively counts the nodes in a chain.
* @param start the first node
* @returns the number of nodes in the linked chain */
public int countNodes(Node start)
{
if (start == null) 

// base case

{
  countNodes (Node start);

// recursive case
else

System.out.println (start.data);
return start.next;
}
} // end countNodes

最佳答案

也许这样想会有所帮助:当前节点的节点数为 1 加上其余节点的计数结果。

关于java - 编写一个递归方法来计算链接节点链中的节点数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3674956/

相关文章:

python - 递归测验 - 无法解决

Java,类的多个实例重置为最新创建的实例

c++ - 使用双链表的电话簿

java - 创建对象枚举

java原始数据类型

java - 如何从Java代码中的数据库表中获取最大ID

java - UISpec4J - AbstractMethodError(UISpecToolkit.createKeyboardFocusManagerPeer)

jquery - 循环,每次迭代仅在 jQuery 延迟之后发生,何时/然后可能没有递归?

ruby - 基因图算法

c - 将项目添加到空链表 C