### 解题思路

双指针算法,定义p1和p2分别指向头节点headA和headB,让p1,p2同时走,如果p1走到尽头,则让p1指向headB,如果p2走到尽头,则让p2指向headA

直到两个指针相遇为止(注:如果两个链表不相交,则p1p2在走到尽头分别交换头节点后,再次走到尽头仍不相遇)

### 代码

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* p1 = headA;
        ListNode* p2 = headB;
        if(p1 == NULL || p2 == NULL) return NULL;
        bool p1flag = false,p2flag = false;
        while(p1 != p2){
            p1 = p1->next;
            p2 = p2->next;
            if(p1 == NULL){
                if(p1flag) break;
                p1 = headB;
                p1flag = true;
            }
            if(p2 == NULL){
                if(p2flag) break;
                p2 = headA;
                p2flag = true;
            }
        }
        if(p1 == p2) return p1;
        return NULL;
    }
};