题解 | #判断一个链表是否为回文结构#
import java.io.;
import java.util.;
class Node{
public int value;
public Node next;
public Node(int data){
this.value=data;
}
public static Node tran(String[]nums){ Node head=new Node(Integer.parseInt(nums[0])); Node cur=head; for(int i=1;i<nums.length;i++){ cur.next=new Node(Integer.parseInt(nums[i])); cur=cur.next; } return head; } public static void printNodeList(Node head){ StringBuilder sb=new StringBuilder(); while(head!=null){ sb.append(head.value).append(" "); head=head.next; } System.out.println(sb.toString()); }
}
public class Main{
public static boolean isPa(Node head){
Stack<node>stack=new Stack<node>();
Node cur=head;
while(cur!=null){
stack.push(cur);
cur=cur.next;
}</node></node>
while(head !=null){ if(head.value!=stack.pop().value){ return false; } head=head.next; } return true; } public static void main(String[]args)throws IOException{ BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); in.readLine(); String[] nums=in.readLine().split(" "); Node head=Node.tran(nums); boolean flag=isPa(head); System.out.println(flag); }
}