Categories
Leetcode Loop

LinkedList Palindrome

let isPalindrome = function(head) {

let slow = head, fast = head, val = [slow.val];

while (fast.next) {
if (fast.next.next) {
fast = fast.next.next;
slow = slow.next;
val.push(slow.val);
}
else {
fast = fast.next;
slow = slow.next;
}
}

while (slow) {
if (slow.val !== val.pop()) {
return false;
}
slow = slow.next;
}

return val.length === 0;
};