【发布时间】:2021-02-10 02:19:25
【问题描述】:
所以基本上我的代码遍历列表,原始方法应该返回链接列表,但是它似乎没有添加我在递归方法中链接的节点,我对为什么感到困惑。谁能帮帮我?
// Append MyStringBuilder2 b to the end of the current MyStringBuilder2, and
// return the current MyStringBuilder2. Be careful for special cases!
public MyStringBuilder2 append(MyStringBuilder2 b)
{
//Test if Invalid
if(b.firstC==null){
return this;
}
//Test if condition is met
else {
CNode lastNode =firstC;
recurseAppendBuild(lastNode, b);
return this;
}
}
private void recurseAppendBuild(CNode lastNode, MyStringBuilder2 BPoint) {
//Test if all nodes have been added
if(lastNode.next==null&&BPoint.firstC==null) {
System.out.println("finished");
}
//Tests if all nodes in the original linked list have been passed through
else if(lastNode.next==null) {
lastNode.next= new CNode(BPoint.firstC.data);
BPoint.firstC=BPoint.firstC.next;
recurseAppendBuild(lastNode.next, BPoint);
}
//Recurse until condition is met
else {
recurseAppendBuild(lastNode.next, BPoint);
}
}
```
【问题讨论】:
-
你不需要递归,你只需要遍历每个元素。递归执行此操作似乎更难,我不知道该怎么做。
-
你的导师规定了递归吗?
-
@NomadMaker 是的,不幸的是,递归是必需的。
-
当您询问学校作业时,请告诉我们。请阅读论坛规则。
-
是的,这是一个学校作业,很抱歉我一定错过了。
标签: java recursion append pass-by-reference singly-linked-list