好青年 | leetcode 打卡群 - 打卡记录第 九 天
给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。
示例 1:
输入: head = [1,2,2,1]
输出: true
示例 2:
输入: head = [1,2]
输出: false
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
var isPalindrome = function (head) {
// 转数组方式
let arr = [];
while (head !== null) {
arr.push(head.val);
head = head.next;
}
let left = 0; right = arr.length - 1;
while (left < right) {
if(arr[left] !== arr[right]) {
return false
}
left++;
right--
}
return true
};