- 栈
- 递归
- 链表
- 双指针
- 回文链表 - 给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。
示例 1:
[https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg]
输入:head = [1,2,2,1] 输出:true
示例 2:
[https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg]
输入:head = [1,2] 输出:false
提示:
- 链表中节点数目在范围[1, 105] 内
- 0 <= Node.val <= 9
进阶:你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
/**
* 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) {
const list = []
let temp = head
while(temp) {
list.push(temp.val)
temp = temp.next
}
const len = list.length
for(let i=0;i<len;i++) {
const isEqual = list[i] === list[len - 1 -i]
if(!isEqual) {
return false
}
}
return true;
};