python-leetcode-相交链表

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]:
        if not headA or not headB:
            return None

        pA, pB = headA, headB

        while pA != pB:
            # 当 pA 走完 A 链表后,跳到 B 链表
            pA = pA.next if pA else headB
            # 当 pB 走完 B 链表后,跳到 A 链表
            pB = pB.next if pB else headA

        return pA  # 若相交返回交点,否则返回 None
相关推荐
小肝一下1 小时前
每日两道力扣,day5
数据结构·c++·算法·leetcode·职场和发展·hot100
jiang_changsheng1 小时前
亚马逊的2026年最新算法变革自然流量分发机制“文本匹配”到“多模态意图理解”的范式革命
大数据·算法·推荐算法
OOJO5 小时前
c++---list介绍
c语言·开发语言·数据结构·c++·算法·list
别或许7 小时前
1、高数----函数极限与连续(知识总结)
算法
派大星~课堂7 小时前
【力扣-142. 环形链表2 ✨】Python笔记
python·leetcode·链表
田梓燊7 小时前
code 560
数据结构·算法·哈希算法
笨笨饿7 小时前
29_Z变换在工程中的实际意义
c语言·开发语言·人工智能·单片机·mcu·算法·机器人
kobesdu7 小时前
综合强度信息的激光雷达去拖尾算法解析和源码实现
算法·机器人·ros·slam·激光雷达
weixin_413063218 小时前
记录 MeshFlow-Online-Video-Stabilization 在线稳像
算法·meshflow·实时防抖
会编程的土豆8 小时前
【数据结构与算法】动态规划
数据结构·c++·算法·leetcode·代理模式