function isPalindrome(list) {
const reversed = reverseAndClone(list);
return isEqual(list, reversed);
}
function reverseAndClone(node) {
let head = null;
while (node) {
const copy = new Node(node.data);
copy.next = head;
head = copy;
node = node.next;
}
return head;
}
function isEqual(one, two) {
while (one && two) {
if (one.data !== two.data) {
return false;
}
one = one.next;
two = two.next;
}
return one === null && two === null;
}
function isPalindromeAlternate(list) {
const set = new Set();
let current = list;
while (current) {
if (set.has(current.data)) {
set.delete(current.data);
} else {
set.add(current.data);
}
current = current.next;
}
return set.size <= 1;
}
function Node(data) {
this.next = null;
this.data = data;
}
Node.prototype.addToTail = function(data) {
const end = new Node(data);
let current = this;
while (current.next !== null) {
current = current.next;
}
current.next = end;
return end;
};