[LeetCode] 234. Palindrome Linked List (easy)

原题

回文 水题

function ListNode(val) {
  this.val = val;
  this.next = null;
}
/**
 * @param {ListNode} head
 * @return {boolean}
 */
var isPalindrome = function(head) {
  var list = [];
  while (head) {
    list.push(head.val);
    head = head.next;
  }
  for (let i = 0; i < (list.length + 1) / 2; i++) {
    if (list[i] !== list[list.length - i - 1]) {
      return false;
    }
  }
  return true;
};

你可能感兴趣的:([LeetCode] 234. Palindrome Linked List (easy))