跳到主要内容

234. [✔][E]回文链表

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false

示例 1:

img

输入:head = [1,2,2,1]
输出:true

示例 2:

img

输入:head = [1,2]
输出:false

提示:

  • 链表中节点数目在范围[1, 105]
  • 0 <= Node.val <= 9

进阶: 你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?


题解:

回文链表

// @lc code=start
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function isPalindrome(head: ListNode | null): boolean {
let left = head;

const traverse = (right: ListNode | null) => {
if (right === null) {
return true;
}
let res = traverse(right.next);
res = res && (right.val === left.val);
left = left.next;
return res;
}

return traverse(head);
};
// @lc code=end