栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

(C语言)160.相交链表【LeetCode】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(C语言)160.相交链表【LeetCode】

ઇଓ 欢迎来阅读子豪的博客(LeetCode刷题篇)

☾ ⋆有什么宝贵的意见或建议可以在留言区留言

ღღ欢迎 素质三连 点赞 关注 收藏

 ❣ฅ码云仓库:补集王子 (YZH_skr) - Gitee.com

160. 相交链表 - 力扣(LeetCode)https://leetcode.cn/problems/intersection-of-two-linked-lists/submissions/

目录

思路

比较长度​编辑

 长的先走

​编辑

 比较结点

总结


思路

O(N^2),先计算两个链表的长度,然后计算长度差,让长的先走差值步

比较长度

 长的先走

 比较结点

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{
    int lenA = 1, lenB = 1,  X = 1, Max = 1;
    struct ListNode* longList; 
    struct ListNode* cura = headA,* curb = headB;
    while(cura->next)
    {
        ++lenA;
        cura = cura->next;
    }
    while(curb->next)
    {
        ++lenB;
        curb = curb->next;
    }
// X表示长度差值
// Max表示长的那个链表的长度
    if(lenA>lenB)
    {
        X=lenA-lenB;
        while(X--)
        {
            headA = headA->next;
        }
        Max = lenA;
    }
    else
    {
        X=lenB-lenA;
        while(X--)
        {
            headB = headB->next;
        }
        Max = lenB;
    }

    while(Max--)
    {
        if(headA == headB)
        return headA;
        headB = headB->next;
        headA = headA->next;
    }
    return NULL;
}

总结

链表的题,结点题不要空想,要用手画图,然后对着实物图来写代码,思路才清晰

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/935192.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号