题目描述:
输入两个链表,找出它们的第一个公共结点。(注意因为传入数据是链表,所以错误测试数据的提示是用其他方式显示的,保证传入数据是正确的)
知识点:
- 链表的遍历
解题思路:
- 这道题是一道比较简单的题,我们给第一个链表打上标记,然后我们来遍历第二个链表,如果在第二个链表中发现了标记直接返回当前节点便好
解题代码:
function FindFirstCommonNode(pHead1, pHead2)
{
// write code here
if(!pHead1 || !pHead2) return null;
while(pHead1) {
pHead1.tag = true;
pHead1 = pHead1.next;
}
while(pHead2) {
if(pHead2.tag) return pHead2
pHead2 = pHead2.next;
}
return null
}