判断链表是否是回文结构
一个数字如果是123321这样子的就是回文数字,一个回文链表的结构是1->2->3->3->2->1这样子,怎么判断这个链表是否是这样的结构,可以借助栈,把一个链表的元素依次放入栈中,如果是回文结构,则栈中取出的顺序和放入的顺序是一致的
package com.test;
import java.util.Stack;
public class Node {
public int val;
public Node next;
public Node(int val){
this.val=val;
}
public boolean HuiwenList(Node node){
Stack<Node> s=new Stack<>();
Node head=node;
while(head!=null){
s.push(head);
head=head.next;
}
while(head!=null){
if(head.val!=s.pop().val){
return false;
}
head=head.next;
return true;
}
return true;
}
}