public class NC96 {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (NC66.ListNode head) {
// write code here
int n = 0;
NC66.ListNode tempNode = head;
while (tempNode != null){
n++;
tempNode = tempNode.next;
}
// 先遍历链表获取链表的长度
if (n == 1){
return true;
// 若长度为1 直接返回true
}else {
Stack<Integer> stack = new Stack<>();
if (n % 2 == 0){
// 长度为偶数时
NC66.ListNode temp1 = head;
for (int i = 1; i <= n / 2 ; i++) {
stack.push(temp1.val);
temp1 = temp1.next;
}
while (temp1 != null && ){
if (stack.pop() != temp1.val){
return false;
}
temp1 = temp1.next;
}
return true;
}else {
// 长度为奇数时
NC66.ListNode temp1 = head;
for (int i = 1; i <= n / 2 ; i++) {
stack.push(temp1.val);
temp1 = temp1.next;
}
temp1 = temp1.next;
while (temp1 != null){
if (stack.pop() != temp1.val){
return false;
}
temp1 = temp1.next;
}
return true;
}
}
}
}