【leetcode】两个链表的第一个公共节点

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
{
    struct ListNode *posA = headA;
    struct ListNode *posB = headB;

    if (!headA || !headB)
        return NULL;

    int count = 0;
    while (posA != posB) {
        posA = posA ? posA->next : headB;
        posB = posB ? posB->next : headA;
        if (count > 2)                    //防止两节点无相交节点无限循环
            return NULL;
        if (posA == headB || posB == headA)
            count++;
    }
    return posA;
}
原文地址:https://www.cnblogs.com/ganxiang/p/13556242.html