/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* FindFirstCommonNode(struct ListNode* pHead1, struct ListNode* pHead2 ) {
// write code here
if(pHead1==NULL||pHead2==NULL)
return NULL;
struct ListNode *p1=pHead1,*p2=pHead2;
int i=0,j=0;
while(1)
{
if(p1==p2)
return p1;
p1=p1->next;
p2=p2->next;
if(p1==NULL)
{
p1=pHead2;
i++;
if(i==2)
return NULL;
}
if(p2==NULL)
{
p2=pHead1;
j++;
if(j==2)
return NULL;
}
}
}