题目描述:
请判断一个链表是否为回文链表。
示例 1:
输入: 1->2 输出: false
示例 2:
输入: 1->2->2->1 输出: true
思路: 转换为数组解决,O(1) 空间复杂度是不可能了。。。
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
var isPalindrome = function(head) {
let res = [];
while(head) {
res.push(head.val);
head = head.next;
}
for(let i = 0, len = res.length; i < len; i++) {
if(res[i] !== res[len - i - 1])
return false;
}
return true;
};