[leetcode] 234. Palindrome Linked List (easy)

版权声明:by ruohua3kou https://blog.csdn.net/ruohua3kou/article/details/82975793

原题
回文 水题

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;
};

猜你喜欢

转载自blog.csdn.net/ruohua3kou/article/details/82975793
今日推荐