fork download
  1. import java.util.*;
  2.  
  3. class Node
  4. {
  5. int data;
  6. Node next;
  7.  
  8. Node(int d)
  9. {
  10. data = d;
  11. next = null;
  12. }
  13. }
  14.  
  15. class Is_LinkedList_Palindrom
  16. {
  17. Node head;
  18.  
  19. /* Function to print linked list */
  20. void printList(Node head)
  21. {
  22. Node temp = head;
  23. while (temp != null)
  24. {
  25. System.out.print(temp.data+" ");
  26. temp = temp.next;
  27. }
  28. System.out.println();
  29. }
  30.  
  31.  
  32. /* Inserts a new Node at front of the list. */
  33. public void addToTheLast(Node node)
  34. {
  35.  
  36. if (head == null)
  37. {
  38. head = node;
  39. } else
  40. {
  41. Node temp = head;
  42. while (temp.next != null)
  43. temp = temp.next;
  44.  
  45. temp.next = node;
  46. }
  47. }
  48. List<Integer> list = new ArrayList<>();
  49. int ctr = 0;
  50. boolean isPalindrome(Node head){
  51. ctr = 0;
  52. list.clear();
  53. return isPalindromeUtil(head);
  54. }
  55. boolean isPalindromeUtil(Node head)
  56. {
  57. if(head==null) return true;
  58. list.add(head.data);
  59. isPalindromeUtil(head.next);
  60. if(head.data == list.get(ctr++)) return true;
  61. return false;
  62. }
  63.  
  64. public static void main(String args[])
  65. {
  66. Scanner sc = new Scanner(System.in);
  67. int t = sc.nextInt();
  68.  
  69. while(t>0)
  70. {
  71. int n = sc.nextInt();
  72. //int k = sc.nextInt();
  73. Is_LinkedList_Palindrom llist = new Is_LinkedList_Palindrom();
  74. //int n=Integer.parseInt(br.readLine());
  75. int a1=sc.nextInt();
  76. Node head= new Node(a1);
  77. llist.addToTheLast(head);
  78. for (int i = 1; i < n; i++)
  79. {
  80. int a = sc.nextInt();
  81. llist.addToTheLast(new Node(a));
  82. }
  83.  
  84. //GfG g = new GfG();
  85. //if(g.isPalindrome(llist.head) == true)
  86. if(llist.isPalindrome(llist.head) == true)
  87. System.out.println(1);
  88. else
  89. System.out.println(0);
  90. t--;
  91. }
  92.  
  93. }
  94. }
  95.  
Success #stdin #stdout 0.12s 2841600KB
stdin
1
45
5 1 1 5 4 3 2 3 3 3 3 3 2 2 1 2 2 1 5 5 5 1 5 2 3 3 2 2 1 5 3 3 2 3 4 2 1 2 4 5 5 4 2 3 5 
stdout
1