题解 | #两个链表的第一个公共结点# 榜一大哥的解法好强

两个链表的第一个公共结点

https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

/**
 * 
 * @param pHead1 ListNode类 
 * @param pHead2 ListNode类 
 * @return ListNode类
 */
struct ListNode* FindFirstCommonNode(struct ListNode* pHead1, struct ListNode* pHead2 ) {
    // write code here
    struct ListNode *cur1,*cur2;
    int flag=0;
    for(cur1=pHead1;cur1!=NULL;cur1=cur1->next)
    {
        for(cur2=pHead2;cur2!=NULL;cur2=cur2->next)
        {
            if(cur1==cur2)
            {
                flag=1;
                break;
            }
            
        }
        if(flag==1)
            break;
    }
    if(flag==1)
    return cur1;
    else
    return NULL;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务