一开始想用双重循环找重复值来做,后来凡发现题意理解错误,并不是找两链表的重复值,而是公共点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *findFirstCommonNode(ListNode *headA, ListNode *headB) {
ListNode *d=headA;
ListNode *p=headB;
while(p!=d)
{
if(p) p=p->next;
else p=headA;
if(d) d=d->next;
else d=headB;
}
return p;
}
};