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.
        
        
    }
};
相关推荐
先吃饱再说1 小时前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰4 小时前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术5 小时前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六8 小时前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术9 小时前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize10 小时前
初识DFS 与 BFS:递归、队列与图遍历
算法
罗西的思考1 天前
机器人 / 强化学习】HIL-SERL:人类在环驱动的具身智能进化框架
人工智能·算法·机器学习
CSharp精选营1 天前
关系型 vs 非关系型:从原理到选型,一文搞定数据库核心分类
数据结构·nosql·关系型数据库·非关系型数据库·技术选型
美团技术团队1 天前
LongCat 开源 VitaBench 2.0:长期动态智能体基准新标杆
人工智能·算法
To_OC2 天前
LC 207 课程表:刚学图论那会儿,我连这是拓扑排序都没看出来
javascript·算法·leetcode