题目链接:https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/
难度:简单

描述:
输入两个链表,找出它们的第一个公共节点。

题解

  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution:
  7. def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
  8. a, b = headA, headB
  9. while a != b:
  10. a = a.next if a else headB
  11. b = b.next if b else headA
  12. return a