题解 | #两个链表的第一个公共结点#

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

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

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

/**
 * 
 * @param pHead1 ListNode类 
 * @param pHead2 ListNode类 
 * @return ListNode类
 */
#include <stdlib.h>
struct ListNode* FindFirstCommonNode(struct ListNode* pHead1, struct ListNode* pHead2 ) {
    struct ListNode* cur=pHead1;
    while (pHead2) {
        while (cur) {
            if (cur==pHead2) {
                return cur;
            }
            cur=cur->next;
        }
        pHead2=pHead2->next;
        cur=pHead1;
    }
    return NULL;
}

全部评论

相关推荐

点赞 评论 收藏
分享
爱吃的猪猪又被画饼了:问问他消息队列怎么保证消息不丢失的,消息堆积你是怎么解决的
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务