题目描述
两链表第一个公共结点
样例
blablabla
算法1
C++ 代码
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node
{
int val;
Node* next;
Node(int _val):val(_val),next(NULL){}
};
Node* findCommonNode(Node* headA,Node* headB)
{
Node* p = headA,*q = headB;
while(p != q)
{
if(!p->next) p = headA;
else p = p->next;
if(!q->next) q = headB;
else q = q->next;
}
return p;
}
int main()
{
//\\\\
findCommonNode(headA,headB);
return 0;
}