fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.stream.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class Ideone {
  8.  
  9. public static boolean isPalindromeList(LinkedList<Integer> list) {
  10. ListIterator<Integer> forward = list.listIterator(0);
  11. ListIterator<Integer> reverse = list.listIterator(list.size());
  12. while (forward.hasNext()) {
  13. if (!forward.next().equals(reverse.previous())) {
  14. return false;
  15. }
  16. }
  17. return true;
  18. }
  19.  
  20. public static void testData(int[] data) {
  21. LinkedList<Integer> list = IntStream.of(data).boxed().collect(LinkedList::new, LinkedList::add, LinkedList::addAll);
  22. boolean isp = isPalindromeList(list);
  23. System.out.println("List " + list + " palindrome " + isp);
  24. }
  25.  
  26. public static void main (String[] args) throws java.lang.Exception {
  27. testData(new int[]{1, 2, 3, 4, 5, 4, 3, 2, 1});
  28. testData(new int[]{1, 2, 4, 5, 4, 3, 2, 1});
  29. }
  30. }
Success #stdin #stdout 0.19s 3359744KB
stdin
Standard input is empty
stdout
List [1, 2, 3, 4, 5, 4, 3, 2, 1] palindrome true
List [1, 2, 4, 5, 4, 3, 2, 1] palindrome false