题目描述

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

  1. # -*- coding:utf-8 -*-
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution:
  7. def FindFirstCommonNode(self, pHead1, pHead2):
  8. # write code here
  9. if not pHead1 or not pHead2:
  10. return None
  11. p1 = pHead1
  12. p2 = pHead2
  13. while p1!=p2:
  14. if p1:
  15. p1 = p1.next
  16. else:
  17. p1 = pHead2
  18. if p2:
  19. p2 = p2.next
  20. else:
  21. p2 = pHead1
  22. return p1