链表力扣复习

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)

相关推荐
inrichtt2 小时前
第十一课时:串口中断接收与回调函数实战解析
c语言·数据结构·stm32·单片机·嵌入式硬件
信奥卷王3 小时前
[GESP202406 六级] 二叉树(含视频)
数据结构·算法
圣保罗的大教堂3 小时前
leetcode 3863. 将一个字符串排序的最小操作次数 中等
leetcode
Tisfy4 小时前
LeetCode 3863.将一个字符串排序的最小操作次数:分类讨论(一次遍历)——简单易懂版
算法·leetcode·字符串·题解·分类讨论
0+1114 小时前
算法 --递归
算法·leetcode
starzy19905 小时前
KafkaProducer 源码深度剖析:支撑高吞吐的数据结构设计
大数据·数据结构·kafka
y1su5 小时前
Leetcode 链表,树,回溯 模板
java·leetcode·链表
罗湖老棍子16 小时前
【例 1】Sightseeing Trip(信息学奥赛一本通- P1494)(洛谷-P10927)
数据结构·算法·图论·最短路·floyd·最小环
文丑颜不良啊17 小时前
LeetCode201~225题目解析
java·leetcode