如何做这个java递归

use*_*578 0 java recursion

我有这个代码:

static int countStu = 0;

public static int countStudent(Node<Student> lst) {
    // pre : true
    // post : res = number of students in list
    if (lst != null) {
        countStu++;
        countStudent(lst.getNext());      
    }
    return countStu;
}
Run Code Online (Sandbox Code Playgroud)

这个方法的问题是我必须countStucountStudent()方法之外声明,这在我想要调用countStudent()两次的情况下不好,它会使返回的值加倍.如何解决此问题并能够countStudent()无限次调用正确的结果?

muh*_*ten 5

相反,return((lst == null)? 0 : (1 + countStudent(lst.getNext()))).