0
点赞
收藏
分享

微信扫一扫

【Leetcode刷题笔记之链表篇】160. 相交链表

Raow1 2022-01-16 阅读 62

😈博客主页:🐼大家好我叫张同学🐼
💖 欢迎点赞 👍 收藏 💗留言 📝 欢迎讨论! 👀
🎵本文由 【大家好我叫张同学】 原创,首发于 CSDN 🌟🌟🌟
精品专栏(不定时更新) 【数据结构+算法】 【做题笔记】【C语言编程学习】
☀️ 精品文章推荐
【C语言进阶学习笔记】三、字符串函数详解(1)(爆肝吐血整理,建议收藏!!!)
【C语言基础学习笔记】+【C语言进阶学习笔记】总结篇(坚持才有收获!)


前言
题目内容

图示两个链表在节点 c1 开始相交:
在这里插入图片描述

在这里插入图片描述

原题链接(点击跳转)

暴力求解法

理解相交:不是A链表中有和B链表中val值相同的结点,而是指这个两个链表中存在相同结点,如果我们通过指针去遍历AB两个链表,能够在AB中找到相同地址结点,那A、B相交否则就是不相交
在这里插入图片描述

函数实现
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode *curA = headA,*curB = headB; 
    while(curA){
        curB = headB;
        while(curB){
            if(curB == curA)
               return curB;
            curB = curB->next;
        }
        curA = curA->next;
    }
    return NULL;
}

在这里插入图片描述


手拉手一起走

这种方式的时间复杂度为O(n^2),空间复杂度是O(1),那能不能想办法改进,降低时间复杂度呢?最好是能实现O(m+n) time,O(1) memory。
在这里插入图片描述
其实要判断两个链表是否相交很简单,我们可以直接判断两个链表的最后一个结点是否相同,若相同则肯定相交,否则就不相交。
但比较麻烦的是我们还需要返回相交的结点,为了实现这个要求,我们可以再遍历A、B两个链表找最后一个结点的时候求A、B的长度lenAlenB。之后让A、B链表中较长的先走 step = | lenA - lenB |(| |表示绝对值)步。然后两个再一起往下走,遇到的第一个相同的结点即为相交结点

函数实现
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    if(headA == NULL || headB == NULL)
        return NULL;
    //判断相交,仅需判断最后一个结点是否相同
    struct ListNode *curA = headA,*curB = headB;
    int lenA = 1,lenB = 1;
    while(curA && curA->next){
        lenA++;
        curA = curA->next;
    }
    while(curB && curB->next){
        lenB++;
        curB = curB->next;
    }
    curA = headA;
    curB = headB;
    if(lenA > lenB){
        int step = lenA - lenB;
        while(step--){
            curA = curA->next;
        }
    }
    else if(lenB > lenA){
        int step = lenB - lenA;
        while(step--){
            curB = curB->next;
        }
    }
    while(curA != curB){
        curA = curA->next;
        curB = curB->next;
    }
    return curA;
}

在这里插入图片描述

利用数学知识

除了刚刚说的思路外,我们也可不用求A、B链表的长度来实现返回相交结点的目的,不过这种方式需要一点点的数学技巧。
在这里插入图片描述

函数实现
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode *curA = headA,*curB = headB;
    while(curA != curB){
        curA = curA == NULL ? headB : curA->next;
        curB = curB == NULL ? headA : curB->next;
    }
    return curA;
}

在这里插入图片描述

举报

相关推荐

0 条评论