【发布时间】:2021-06-13 04:12:45
【问题描述】:
我目前正在研究链表中的递归,我有一个问题,我必须比较链表中的两个数字,并且我必须递归地进行。
如果列表中的所有元素都按升序排列,则返回true。
这是我在列表中所做的:
public boolean ascending() {
if(this.first == null) return false;
return this.first.ascending();
}
这是我在细胞中所做的:
public boolean ascending() {
if(this.next != null) {
if(this.employee.getNumbers() < this.next.employee.getNumbers()) return true;
this.next.ascending();
} return false;
}
问题是我的代码对 100
有什么建议吗?提前谢谢!
【问题讨论】:
-
你的代码有一个错误:如果你在递归调用你的方法之前返回true,那么你永远不会进入递归
标签: java recursion linked-list