算法(TS):回文链表

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

示例 1:

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

示例 2:

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


提示:

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

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

解答一

将链表中节点保存到一个Map中,key 为节点的次序,value 为节点。对比 Map 中第 n 个节点与第 total - n -1 个节点的值是否相等。

ts 复制代码
/**
 * 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 {
    if (!head) return false
    const nodeMap = new Map<number,ListNode>()
    let count = 0
    while(head) {
        nodeMap.set(count,head)
        head = head.next
        count++
    }

    const total = count
    count = 0
    const middle = Math.floor(total/2)
    while(count < middle) {
        if (nodeMap.get(count).val !== nodeMap.get(total-count-1).val) {
            return false
        }
        count++
    }

    return true
};

**

**

时间复杂度O(n + n/2),空间复杂度O(n)

解答二

  1. 定义两个指针,fast 和 low,fast 的移动速度是 low 的两倍,当fast移动到链表末尾时,low就移动到了链表中间
  2. 反转 low 到链表末尾这部分节点
  3. 将 fast 重置到链表头,同步移动fast和low,比较他们指向的节点值是否相等,不相同则不是回文链表
ts 复制代码
/**
 * 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 {
    if (!head) return false
    let fast = head
    let low = head
    while(fast && fast.next) {
        fast = fast.next.next
        low = low.next
    }
    // 如果 fast 不为 null,则说明传入的链表节点数为奇数
    if (fast) {
        low = low.next
    }

    low = reserve(low)
    fast = head
    while(low) {
        if (fast.val !== low.val) {
            return false
        }
        low = low.next
        fast = fast.next
    }

    return true
};

function reserve(head: ListNode):ListNode {
    let prev = null
    let current = null
    while(head) {
        prev = current
        current = head
        head = head.next
        current.next = prev
    }

    return current
}

时间复杂度O(n),空间复杂度O(1)

相关推荐
努力学算法的蒟蒻23 分钟前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展
2401_8414956428 分钟前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
AC赳赳老秦30 分钟前
2026国产算力新周期:DeepSeek实战适配英伟达H200,引领大模型训练效率跃升
大数据·前端·人工智能·算法·tidb·memcache·deepseek
2401_841495641 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
budingxiaomoli1 小时前
优选算法-字符串
算法
qq7422349841 小时前
APS系统与OR-Tools完全指南:智能排产与优化算法实战解析
人工智能·算法·工业·aps·排程
A尘埃2 小时前
超市购物篮关联分析与货架优化(Apriori算法)
算法
.小墨迹2 小时前
apollo学习之借道超车的速度规划
linux·c++·学习·算法·ubuntu
不穿格子的程序员2 小时前
从零开始刷算法——贪心篇1:跳跃游戏1 + 跳跃游戏2
算法·游戏·贪心
大江东去浪淘尽千古风流人物2 小时前
【SLAM新范式】几何主导=》几何+学习+语义+高效表示的融合
深度学习·算法·slam