Java 返回第k个到最后一个:输出不正确

Java 返回第k个到最后一个:输出不正确,java,algorithm,data-structures,linked-list,singly-linked-list,Java,Algorithm,Data Structures,Linked List,Singly Linked List,我正在研究一个简单的算法,试图从线性链表中找到最后一个元素的第k个元素 然而,在我的解决方案中,它并没有输出我期望的正确数字 我知道如何解决这个问题,但我想知道为什么head递归不能按我的预期方式工作。如果我能再得到一双眼睛,我会很感激的 包装函数 public int findKthToLast(int kth){ //If zero items in the list if(head == null){ System.out.print

我正在研究一个简单的算法,试图从线性链表中找到最后一个元素的第k个元素 然而,在我的解决方案中,它并没有输出我期望的正确数字

我知道如何解决这个问题,但我想知道为什么head递归不能按我的预期方式工作。如果我能再得到一双眼睛,我会很感激的

包装函数

public int findKthToLast(int kth){
        //If zero items in the list
        if(head == null){
            System.out.println("List is empty");
            return 0;
        }
        //If 1 item in the list
        if(head.getNext() == null){
            System.out.println("Only 1 item in the list which is: " + head.getData());
            return 0;
        }
        //Allocating an array of size 1. This will help me keep track on what kth element when I go back from the recursion
        int[] array = new int[1];
        array[0] = -1;

        //the '1' below represents the length. it will increment as you see in the recursive solution
        return findKthToLast(head,kth,1,array);
    }
递归

public int findKthToLast(Node head,int kth,int length, int [] array){
        //if final item in the list
        if(head.getNext() == null){
            //if kth element is greater then total length just return 0
            if(kth >= length){
                return 0;
            }
            //kth = 0 means output the final item in the list. returns head.data
            if(kth == 0){
                return head.getData();
            }
            //when I backtrack from the stack I need to know if the kth to final element is equal to length. That's where the array comes from
            array[0] = length - kth;
            return 0;
        }
        int element;
        element = findKthToLast(head.getNext(),kth,++length,array);
         
        //if equal then I'm done. return the head.data
        if(length == array[0]){
            return head.getData();
        }
        return element;

    }
问题是:

在列表中:8->4->2->1。如果kth=1(我希望项目位于最后一个项目之前,因此在本例中,值为“2”),则输出应为“2”。但是,在我当前的代码中,我收到了一个更高的数字,因此值为“4”

我不想要正确的代码。我知道如果我把基本情况从

if(head.getNext() == null)
to 
if(head == null)

那么我的代码就完全正常了。我想要的是为什么我当前的解决方案不起作用。我是否错误地可视化了调用堆栈?谢谢

您可能比自己聪明,因为您在每次递归调用中都有一种非常反常的计算列表长度的方法。您修改了length变量,以便将增加的长度正确地传递到下一个递归调用中,而不是仅向当前长度添加一个变量。。。然而,当你的函数弹出时,你会使用增加的长度,导致你计算错误

让我们逐步完成以下示例:

8 -> 4 -> 2 -> 1
findKthToLast(8, 1, 1, [-1])
|
|-> 8.next is NOT null
    |
    | element = findKthToLast(4, 1, ++1, [-1])
      |
      | -> 4.next is NOT null
           |
           | element = findKthToLast(2, 1, ++2, [-1])
             |
             | -> 2.next is NOT null
                  |
                  | element = findKthToLast(1, 1, ++3, [-1])
                    |
                    | -> 1.next IS null
                         | kth is NOT 0
                         | => array[0] <- 4 - 1 = 3 (correct)
                         | return 0
                    | element = 0
                    | length is 4 != 3 because of prefix increment (length should be 3 but you incremented before calling the function)
                    | return 0
             | element = 0
             | length is 3 == 3 because of prefix increment, so return current node
             | return 2 (not correct but this is what you told the code to do)
      | element = 2
      | length is 2 != array[0] 
      | return 2
| return 2 
8->4->2->1
findKthToLast(8,1,1,[-1])
|
|->8.next不为空
|
|element=findKthToLast(4,1,+1,[-1])
|
|->4.next不为空
|
|element=findKthToLast(2,1,+2,[-1])
|
|->2.next不为空
|
|元素=findKthToLast(1,1,+3,[-1])
|
|->1.next为空
|kth不是0

|=>阵列[0]是否已完成任何调试?你的发现是什么?