https://leetcode.cn/problems/kth-node-from-end-of-list-lcci
思路:先通过快慢指针找出中心点,然后中心点之后的节点链表反转,再通过头部和尾部,逐渐向中间遍历,相遇则结束(注意偶数节点个数),因为中心点之后的链表实际上是环状,偶数没法相遇只能通过 left.next = right 来判断结束
 {
if(head == null || head.next == null){
return true;
}
// write code here
ListNode fast = head;
ListNode slow = head;
//快慢指针判断中间值
while(fast != null && fast.next != null){
//快指针走两步
fast = fast.next.next;
//快指针走两步
slow = slow.next;
}
//cur指向中间值下一个节点
//slow中间值
ListNode cur = slow.next;
while(cur != null){
//curNext指向cur的下一个节点
ListNode curNext = cur.next;
//让cur.next指向slow
cur.next = slow;
//slow移到cur位置
slow = cur;
//cur移到curNext位置
cur = curNext;
}
//定义左指针
ListNode left = head;
//定义右指针
ListNode right = slow;
//相遇时停止
while(left != right){
//不相等不是回文数
if(left.val != right.val){
return false;
}
//注意偶数情况,偶数没法相遇,针对偶数进行判定
//不能放到比较值前面,应该先比较值,在比较节点
if(left.next == right){
return true;
}
//右移
left = left.next;
//左移
right = right.next;
}
//返回true
return true;
}
}