题目描述:

输入两个链表,找出它们的第一个公共结点。(注意因为传入数据是链表,所以错误测试数据的提示是用其他方式显示的,保证传入数据是正确的)

知识点:

  • 链表的遍历

解题思路:

  • 这道题是一道比较简单的题,我们给第一个链表打上标记,然后我们来遍历第二个链表,如果在第二个链表中发现了标记直接返回当前节点便好

解题代码:

  1. function FindFirstCommonNode(pHead1, pHead2)
  2. {
  3. // write code here
  4. if(!pHead1 || !pHead2) return null;
  5. while(pHead1) {
  6. pHead1.tag = true;
  7. pHead1 = pHead1.next;
  8. }
  9. while(pHead2) {
  10. if(pHead2.tag) return pHead2
  11. pHead2 = pHead2.next;
  12. }
  13. return null
  14. }