-
问题描述
判断单链表是否是回文结构。 -
解决方案1
将链表中的数据全部压入堆栈,然后再依次弹出和原来的链表进行对比。
/**
* 全部压栈,然后依次弹出对比
* @param head
* @return
*/
public static boolean isPalindrome1(Node head){
Node cur = head;
Stack<Integer> stack = new Stack<>();
while(cur != null){
stack.push(cur.value);
cur = cur.next;
}
cur = head;
while(cur != null){
if(cur.value != stack.pop()