欢迎来到 s a y − f a l l 的文章 欢迎来到say-fall的文章 欢迎来到say−fall的文章
文章目录
题目:相交链表
- 流程图:
- 具体步骤:
- 假设的链表图:
- 代码:
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;
}
- 本节完…
618

被折叠的 条评论
为什么被折叠?



