Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use recursion in Java is not right
import java.util.*;
public class RecursionMax {
public static Integer max(LinkedList<Integer> numberList, Integer maxNumber) {
if (numberList.size() == 0) {
return maxNumber;
} else {
Integer firstNumber = numberList.get(0);
if (maxNumber < firstNumber) {
maxNumber = firstNumber;
}
numberList.removeFirst();
return max(numberList, maxNumber);
}
}
public static void main(String[] args) {
LinkedList<Integer> numberList = new LinkedList<Integer>();
numberList.add(4);
numberList.add(3);
numberList.add(10);
numberList.add(6);
numberList.add(998);
// 998
System.out.println(RecursionMax.max(numberList, 0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment