链表力扣复习

160. 相交链表 - 力扣(LeetCode)

python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
        a,b = headA,headB
        while a is not b:
            a = a.next if a else headB
            b = b.next if b else headA
        return a

本质:交换跑道,消除长度差

易错点及注意事项:

① 为什么不相交也能正常结束?各走 m+n 步后同时变成 None,a is not b 不成立;

② 比较链表节点时,必须用 is 比较节点对象,不能比 val;

206. 反转链表 - 力扣(LeetCode)

python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: ListNode | None) -> ListNode | None:
        prev,cur = None,head
        while cur:
            // 保存下一个节点
            nxt = cur.next
            // 反转当前节点指针
            cur.next = prev
            // 前移 prev 指针
            prev = cur
            // 前移 cur 指针
            cur = nxt
        return prev

易错点及注意事项:

① 最终返回结果为 prev 而非 head,因为反转后此时 head 已是某个尾节点;

② 四行循环体顺序不能乱,先存 nxt 是第一步;

234. 回文链表 - 力扣(LeetCode)

python 复制代码
class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        # 1. 快慢指针找中点(偶数长度时 slow 停在中间偏右)
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        # 2. 反转后半段(从 slow 开始)
        prev, cur = None, slow
        while cur:
            nxt = cur.next
            cur.next = prev
            prev = cur
            cur = nxt
        # 3. 从两端向中间比对,以后半段(较短或等长)为准
        first, second = head, prev
        while second:
            if first.val != second.val:
                return False
            first = first.next
            second = second.next
        return True

**口诀:**找中点 + 反转 + 双指针比对

易错点及注意事项:

① 第三步 "比对" 时,循环条件用 while second,反转出的后半段一定不长于前半段,走完即可判定结束;

② 追问 "要求不修改链表应当如何处理?比对完再将后半段反转回去接上;

141. 环形链表 - 力扣(LeetCode)

python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        slow=fast=head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow is fast:
                return True
        return False

142. 环形链表 II - 力扣(LeetCode)

python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow=fast=head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow is fast:
                p=head
                while p is not slow:
                    p = p.next
                    slow = slow.next
                return p
        return None

2. 两数相加 - 力扣(LeetCode)

相关推荐
Go-higher6 小时前
C语言--数据结构-栈、队列、堆
c语言·数据结构
郝学胜-神的一滴1 天前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
旖旎夜光1 天前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
Regulus-Moon1 天前
Quorum:用一个不等式换掉分布式锁
数据结构·论文阅读·分布式·算法·系统架构
老师我太想进步了20261 天前
【数据结构-16】链式二叉树在线OJ题
c语言·数据结构·算法
程序员大阳1 天前
副队长C语言数据结构教程(4)--双向链表
c语言·数据结构·链表·双向链表
木井巳1 天前
【动态规划: 子序列问题】最长递增子序列的个数
java·算法·leetcode·动态规划·推荐算法
Navigator_Z1 天前
LeetCode //C - 1326. Minimum Number of Taps to Open to Water a Garden
c语言·算法·leetcode
XW9632582 天前
除湿机行业组织能力与人才地图报告
大数据·服务器·网络·数据结构·数据库·人工智能
Rain的Java大神之路2 天前
LeetCode 45 跳跃游戏 II:大厂面试官最爱追问的贪心题,我把骨头给你拆干净了
java·后端·leetcode