/**
* 判断链表是否为回文:
* 利用链表逆序的思路,将中点之后的指针反序指向中点,从两头走
* 直到走到null依旧没有返回false,那就是会回文
*
* 2 -> 6 -> 9 <- 6 <- 2
* |
* null
* slow = head
* fast = head
*/
//反转后一半链表前后比对,然后调整好指针
private static boolean isPalindrome(Node head) {
if(head == null || head.next == null){
return false;
}
// 0 0 0
// 1
Node slow = head;
Node fast = head;
//利用快慢指针找到链表中点,偶数长度返回上中点,奇数返回中点
while (fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
}
Node curr = slow;
Node pre = null;
//从找到的中点slow节点开始反转后一半链表
while (curr != null){
slow = curr.next;
curr.next = pre;
pre = curr;