/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
var isPalindrome = function (head) {
// 解法一:
// 复制链表值到数组列表中。
// 使用双指针法判断是否为回文
// let stack = []
// while (head) {
// stack.push(head.val)
// head = head.next
// }
// for (let i = 0, j = stack.length - 1; i < j; i++, j--) {
// if (stack[i] != stack[j]) {
// return false
// }
// }
// return true
// 解法二
// 1:快慢指针同时移动,慢指针移动一步,快指针移动两步;直到快指针为null时,则找到了中位数
// 2:从中位数的下一位进行反转
// 3:然后p1指向head p2指向中位数的下一位进行比较,不相等返回false,相等返回true
const reverseList = (head) => {
let prev = null;
let curr = head;
while (curr !== null) {
let nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
const endOfFirstHalf = (head) => {
let fast = head;
let slow = head;
while (fast.next !== null && fast.next.next !== null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
var isPalindrome = function (head) {
if (head == null) return true;
// 找到前半部分链表的尾节点并反转后半部分链表
const firstHalfEnd = endOfFirstHalf(head);
const secondHalfStart = reverseList(firstHalfEnd.next);
// 判断是否回文
let p1 = head;
let p2 = secondHalfStart;
let result = true;
while (result && p2 != null) {
if (p1.val != p2.val) result = false;
p1 = p1.next;
p2 = p2.next;
}
// 还原链表并返回结果
firstHalfEnd.next = reverseList(secondHalfStart);
return result;
};
};