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

方法1:暴力法

时间复杂度 :O(m×n)
空间复杂度:O(1)

python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 13:10 
# "Stay hungry,stay foolish."

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

# 暴力破解 超出时间限制
class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        # 2026 - 1 - 5 错题集
        currentA = headA
        while currentA: # 遍历到最后currentA为None,while currentA:为False,循环退出
            currentB = headB
            while currentB:
                if currentA == currentB:
                    return currentA
                currentB = currentB.next
            currentA = currentA.next
        return None

方法2:哈希表法

时间复杂度 :O(m+n)
空间复杂度:O(m) 或 O(n)

python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 13:10 
# "Stay hungry,stay foolish."

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

# 哈希表法
class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        # 2026 - 1 - 5 错题集
        seen = set()
        currnt = headA
        while currnt:
            seen.add(currnt)
            currnt = currnt.next

        currnt = headB
        while currnt:
            if currnt in seen:
            # if current in seen_list 这个操作在列表中是线性查找(O(m)),导致整体复杂度为 O(m×n),效率低下。
                return currnt
            currnt = currnt.next
        return None

方法3:双指针法

时间复杂度 :O(m+n)
空间复杂度:O(1)

python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 13:10 
# "Stay hungry,stay foolish."

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

# 双指针法
class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        # 2026 - 1 - 5 错题集
        p1, p2 = headA, headB
        while p1 != p2:
            if p1:
                p1 = p1.next
            else:
                p1 = headB
            if p2:
                p2 = p2.next
            else:
                p2 = headA

        # 相交点或null(不相交时pA/pB均为null)
        return p1

结果

解题步骤1:www.youtube.com

解题步骤2:www.bilibili.com

相关推荐
YuTaoShao30 分钟前
【LeetCode 每日一题】1653. 使字符串平衡的最少删除次数——(解法三)DP 空间优化
算法·leetcode·职场和发展
cpp_25011 小时前
P10570 [JRKSJ R8] 网球
数据结构·c++·算法·题解
cpp_25011 小时前
P8377 [PFOI Round1] 暴龙的火锅
数据结构·c++·算法·题解·洛谷
TracyCoder1231 小时前
LeetCode Hot100(26/100)——24. 两两交换链表中的节点
leetcode·链表
季明洵1 小时前
C语言实现单链表
c语言·开发语言·数据结构·算法·链表
only-qi2 小时前
leetcode19. 删除链表的倒数第N个节点
数据结构·链表
cpp_25012 小时前
P9586 「MXOI Round 2」游戏
数据结构·c++·算法·题解·洛谷
浅念-2 小时前
C语言编译与链接全流程:从源码到可执行程序的幕后之旅
c语言·开发语言·数据结构·经验分享·笔记·学习·算法
爱吃生蚝的于勒2 小时前
【Linux】进程信号之捕捉(三)
linux·运维·服务器·c语言·数据结构·c++·学习
数智工坊3 小时前
【数据结构-树与二叉树】4.6 树与森林的存储-转化-遍历
数据结构