JavaScript Tutorial ✦
Check if a Linked List is Palindrome
Function to determine if a linked list reads the same forwards and backwards.
function isPalindrome(head) {
let fast = head, slow = head, stack = [];
while (fast && fast.next) {
stack.push(slow.value);
slow = slow.next;
fast = fast.next.next;
}
if (fast) slow = slow.next;
while (slow) {
if (slow.value !== stack.pop()) return false;
slow = slow.next;
}
return true;
}