160.相交链表

思路

暴力法:

你看到题目,其实就是判断是否有两个元素相同,暴力法,对于链表A中的每一个节点,遍历链表B的所有节点,检查是否有节点地址相同的。

时间复杂度: O(L_A * L_B)

空间复杂度: O(1)

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * ListNode *next;
 * ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        // 暴力法
        // 先判断边界情况
        if (headA == nullptr || headB == nullptr) {
            return nullptr;
        }

        // 固定,遍历
        // 初始节点
        ListNode *ptA = headA;
        //flag 标志是否找到
        while (ptA != nullptr) {
            ListNode *ptB = headB;
            while (ptB != nullptr) {
                if (ptB == ptA) {
                    return ptB;
                }
                ptB = ptB->next;
            }
            ptA = ptA->next;
        }
        return nullptr;
    }
};

哈希集合法

其实很多判断两者是否相等的,都可以使用hash集合,因为hash的特点就是取值时间复杂度为O(1). 我们可以先遍历A链表,把这个链表的所有节点的地址存储到hash中,然后再遍历链表B,依次判断是否有节点地址和A中的相同。

时间复杂度: O(L_A + L_B)

空间复杂度: O(L_A) # 因为存储了hash

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * ListNode *next;
 * ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        // 哈希集合法 find, count, insert 时间复杂度为O(1)
        // 先判断边界情况
        if (headA == nullptr || headB == nullptr) {
            return nullptr;
        }

        // 定义集合
        unordered_set<ListNode*> nodeset;

        // 遍历链表A
        ListNode *ptA = headA;
        while (ptA != nullptr) {
            if (nodeset.find(ptA) == nodeset.end()) {
                nodeset.insert(ptA);
                ptA = ptA -> next;
            }
        }

        // 遍历链表B
        ListNode *ptB = headB;
        while (ptB != nullptr) {
            if (nodeset.find(ptB) != nodeset.end()) {
                return ptB;
            }
            ptB = ptB->next;
        }
        return nullptr;

        
    }
};

倒序法

因为如果两个链表相交了,那么他们在链表的末端肯定也是重叠的,那么其实我只要判断最后一个节点是否相等就知道是否相交了,但是也需要找到相交的起点,如果可以倒着遍历就解决了(换一个想法,我可以正向遍历的时候,用vector分别把A,B链表的节点地址存储下来,然后倒着对齐遍历这两个vector,一旦开始不相等,那么前一个地址就是相交点)

时间复杂度: O(LA+LB)

空间复杂度: O(LA+LB)

长度差法

这个方法其实和倒序法基本一样,也是考虑末端是重叠的,那么我只要计算出两个链表的长度,然后将长链表的起点前移,使得两个链表的剩余长度相等,最后再同步遍历寻找交点。

时间复杂度: O(LA+LB)

空间复杂度: O(1)

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * ListNode *next;
 * ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        // 长度差法
        // 先判断边界情况
        if (headA == nullptr || headB == nullptr) {
            return nullptr;
        }
        // 遍历A
        ListNode *ptA = headA;
        ListNode *ptB = headB;
        int length_A = 0;
        int length_B = 0;
        while (ptA != nullptr) {
            length_A = length_A + 1;
            ptA = ptA -> next;
        }
        while (ptB != nullptr) {
            length_B = length_B + 1;
            ptB = ptB -> next;
        }
        int diff = length_A - length_B;
        ListNode *longer = headA;
        ListNode *shorter = headB;

        if (length_A < length_B) {
            longer = headB;
            shorter = headA;
            diff = -1 * diff;
        }
        // 让长的先走
        for (int i = 0; i < diff; i++) {
            longer = longer->next;
        }
        while(longer != nullptr) {
            if (longer == shorter) {
                return longer;
            }
            longer = longer->next;
            shorter = shorter->next;
        }
        return nullptr;

        
        
    }
};

方法:双指针法

由于相交,那么当A链表走完之后还走B链表,B链表走完之后还走A链表的话,并且他们同时出发,那么如果有交点,那么肯定在交点出相遇

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * ListNode *next;
 * ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        // 长度差法
        // 先判断边界情况
        if (headA == nullptr || headB == nullptr) {
            return nullptr;
        }
        ListNode *ptA = headA;
        ListNode *ptB = headB;

        while (ptA != ptB) {
            ptA = (ptA == nullptr) ? headB : ptA->next;

            ptB = (ptB == nullptr) ? headA : ptB->next;

        }
        return ptA; // 因为遍历完成后,要么就是要的交点,要么就是nullptr.
        
        
    }
};
相关推荐
小O的算法实验室17 小时前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
‎ദ്ദിᵔ.˛.ᵔ₎17 小时前
LIST 的相关知识
数据结构·list
M--Y17 小时前
Redis常用数据类型
数据结构·数据库·redis
郭涤生18 小时前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿18 小时前
vector
c语言·开发语言·数据结构·c++·算法
Ricky111zzz18 小时前
leetcode学python记录1
python·算法·leetcode·职场和发展
汀、人工智能18 小时前
[特殊字符] 第58课:两个正序数组的中位数
数据结构·算法·数据库架构··数据流·两个正序数组的中位数
liu****18 小时前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯
汀、人工智能18 小时前
[特殊字符] 第79课:分割等和子集
数据结构·算法·数据库架构·位运算·哈希表·分割等和子集
汀、人工智能18 小时前
[特殊字符] 第74课:完全平方数
数据结构·算法·数据库架构·图论·bfs·完全平方数