// 用栈的方法,把链表放入栈中,再弹出来同时打印对应链表值对比看是否相等
public static Boolean IsHuiWen(Node head){
if(head == null || head.next == null){
return true;
}
Node temp =head;
Stack<Node> stack = new Stack<>();
// 链表 入栈
while(temp != null){
stack.push(temp);
temp = temp.next;
}
while(!stack.isEmpty()){
Node pop = stack.pop();
if(pop.value != head.value){
return false;
}
head = head.next;
}
return true;
}
// 用快慢指针找出中点,一半部分入栈
public static Boolean isHuiWen2(Node head){
if(head == null || head.next == null){
return true;
}
// 快慢指针,快指针结束后,慢指针就在中点
Node slow = head;
Node fast = head;
while(slow.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
}
Stack<Node> stack = new Stack<>();
while(slow != null){
stack.push(slow);
slow = slow.next;
}
while(!stack.isEmpty()){
if(stack.pop().value != head.value){
return false;
}
head = head.next;
}
return true;
}