70 - 回文链表
题目
解答
var isPalindrome = function (head) {
if (!head || !head.next) {
return true
}
// 快慢指针
let fast = head.next.next
slow = head.next
while (fast && fast.next) {
fast = fast.next.next
slow = slow.next
}
// 翻转前半部分链表
let pre = null
next = null
while (head != slow) {
next = head.next
head.next = pre
pre = head
head = next
}
if (fast) {
slow = slow.next
}
// 回文校验
while (pre) {
if (pre.val != slow.val) {
return false
}
pre = pre.next
slow = slow.next
}
return true
};Last updated