题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
ListNode* l1=pHead1;
ListNode* l2=pHead2;
//l1到了链表末尾,指向pHead2;
//l2到了链表末尾,指向pHead1;
while(l1!=l2){
if(l1!=nullptr){
l1=l1->next;
}
else{
l1=pHead2;
}
if(l2!=nullptr){
l2=l2->next;
}
else{
l2=pHead1;
}
}
return l1;
}
};
腾讯成长空间 6074人发布