fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static <T> T recursiveLast(ListIterator<T> iter) {
  11. if (!iter.hasNext()) {
  12. // It is invalid to call recursiveLast on an empty list
  13. return null;
  14. }
  15. T val = iter.next();
  16. if (iter.hasNext()) {
  17. val = recursiveLast(iter);
  18. }
  19. iter.previous();
  20. iter.set(val);
  21. return val;
  22. }
  23. public static void main (String[] args) throws java.lang.Exception
  24. {
  25. List<Integer> list = new ArrayList<Integer>();
  26. list.add(1);
  27. list.add(2);
  28. list.add(3);
  29. recursiveLast(list.listIterator());
  30. for (Integer i : list) {
  31. System.out.println(i);
  32. }
  33. }
  34. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
3
3
3