
🌈这里是say-fall分享,感兴趣欢迎三连与评论区留言 🔥专栏: 《C语言从零开始到精通》 《C语言编程实战》 《数据结构与算法》 《小游戏与项目》 💪格言:做好你自己,你才能吸引更多人,并与他们共赢,这才是你最好的成长方式。




typedef struct ListNode ListNode;
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
{
ListNode* pcura = headA;
ListNode* pcurb = headB;
int lena = 1;
int lenb = 1;
while(pcura->next)
{
pcura = pcura->next;
lena++;
}
while(pcurb->next)
{
pcurb = pcurb->next;
lenb++;
}
//没有相同尾节点,返回NULL
if(pcurb != pcura)
{
return NULL;
}
//长的走差距步
ListNode* longNode = NULL;
ListNode* stortNode = NULL;
int len = 0;
if(lena>lenb)
{
longNode = headA;
stortNode = headB;
len = lena - lenb;
}
else
{
longNode = headB;
stortNode = headA;
len = lenb - lena;
}
while(len--)
{
longNode = longNode->next;
}
while(longNode)
{
if(longNode == stortNode)
{
return longNode;
}
longNode = longNode->next;
stortNode = stortNode->next;
}
return NULL;
}