题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
#include <vector>
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
vector<ListNode*> list1;
ListNode* p = pHead1;
while (p != nullptr) {
list1.push_back(p);
p = p->next;
}
ListNode* q = pHead2;
while (q != nullptr) {
for (auto& i : list1) {
if (i == q) {
return q;
}
}
q = q->next;
}
return nullptr;
}
};
