fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. class Main{
  5. static class Node{
  6. int data;
  7. Node next;
  8. Node(int data){
  9. this.data = data;
  10. }
  11. }
  12. static Node buildlist(int[] vals){
  13. Node head = null,tail=null;
  14. for(int val : vals){
  15. Node newNode = new Node(val);
  16. if(head == null){
  17. head = newNode;
  18. tail = newNode;
  19. }else{
  20. tail.next = newNode;
  21. tail = newNode;
  22. }
  23. }
  24. return head;
  25. }
  26. static Node reverse(Node head){
  27. Node curr=head.next; Node prev = null;
  28. while(curr!=null){
  29. Node next = curr.next;
  30. curr.next = prev;
  31. prev = curr;
  32. curr = next;
  33. }
  34. return head;
  35. }
  36. static boolean check(Node head,Node head1){
  37. Node curr = head.next,curr1 = head1.next;
  38. while(curr.next!=null&&curr1.next!=null){
  39. if(curr != curr1) return false;
  40. curr = curr.next;
  41. curr1 = curr1.next;
  42. }
  43. return true;
  44. }
  45. public static void main(String[]args){
  46. Scanner cs = new Scanner(System.in);
  47. int n = cs.nextInt();
  48. int[] arr = new int[n];
  49. for(int i=0; i<n; i++){
  50. arr[i] = cs.nextInt();
  51. }
  52. Node head = buildlist(arr);
  53. Node head1 = reverse(head);
  54.  
  55. System.out.println(check(head,head1)? "yes" : "no");
  56. }
  57. }
Success #stdin #stdout 0.12s 56544KB
stdin
4
1 2 3 4
stdout
yes