Java linkedlist上的递归:如何比较?

Java linkedlist上的递归:如何比较?,java,recursion,linked-list,Java,Recursion,Linked List,我目前正在研究链表中的递归,我有一个问题,我必须比较链表中的两个数字,我必须递归地进行比较 如果列表中的所有元素都按升序排列,则返回true 下面是我在列表中所做的: public boolean ascending() { if(this.first == null) return false; return this.first.ascending(); } 以下是我在牢房里所做的: public boolean ascending() {

我目前正在研究链表中的递归,我有一个问题,我必须比较链表中的两个数字,我必须递归地进行比较

如果列表中的所有元素都按升序排列,则返回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;
    }
公共布尔升序(){
if(this.next!=null){
if(this.employee.getNumbers()
问题是我的代码在100<200时返回true,但在100<200<300时返回false,在100<200<100时返回true


有什么建议吗?提前谢谢你

你需要改变你的逻辑

您根本不想返回true,除非this.next为null,因为这意味着您的数字已用完

你想做的是:只要我有数字,下一个比当前的大,就继续看。否则返回false。如果我用完了数字,则返回true。

如解释所示,您需要以下内容:

public boolean ascending() 
{
        if(this.next == null)
           return true;
        else if(this.employee.getNumbers() < this.next.employee.getNumbers()) {
           return this.next.ascending();
        } 
        else return false;
 }
公共布尔升序()
{
if(this.next==null)
返回true;
else if(this.employee.getNumbers()
您的代码有一个错误:如果在递归调用方法之前返回true,则永远不会输入recursionAwesome,我太懒了,无法编写代码。谢谢你从我结束的地方接过来谢谢你的建议!我试着这样做,但是结果总是错误的。我也不明白,如果next为null,为什么要返回true。“我不应该是假的吗?”伊芙琳,现在试试看。如果您到达末尾,这意味着在此之前,所有按您希望的顺序排序的数字,否则在它最终工作之前,您将返回false!非常感谢你我为此挣扎了一段时间question@Evelyne实际上我有一个更好的方法