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

相关推荐
2401_892070984 小时前
链栈(链式栈) 超详细实现(C 语言 + 逐行精讲)
c语言·数据结构·链栈
语戚6 小时前
力扣 968. 监控二叉树 —— 贪心 & 树形 DP 双解法递归 + 非递归全解(Java 实现)
java·算法·leetcode·贪心算法·动态规划·力扣·
skywalker_116 小时前
力扣hot100-7(接雨水),8(无重复字符的最长子串)
算法·leetcode·职场和发展
CoderCodingNo7 小时前
【GESP】C++三级真题 luogu-B4499, [GESP202603 三级] 二进制回文串
数据结构·c++·算法
田梓燊8 小时前
leetcode 160
算法·leetcode·职场和发展
_深海凉_8 小时前
LeetCode热题100-颜色分类
python·算法·leetcode
网安INF8 小时前
数据结构第三章:栈、队列和数组
数据结构
6Hzlia8 小时前
【Hot 100 刷题计划】 LeetCode 136. 只出现一次的数字 | C++ 哈希表&异或基础解法
c++·算法·leetcode
yuannl109 小时前
数据结构----双端队列实现
数据结构
无限进步_10 小时前
【C++】只出现一次的数字 II:位运算的三种解法深度解析
数据结构·c++·ide·windows·git·算法·leetcode