LeetCode 160. 相交链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        auto p=headA,q=headB;
        while(p!=q)
        {
            if(!q)  q=headA;
            else q=q->next;
            if(!p)  p=headB;
            else p=p->next;
        }
        return p;
    }
};
posted @ 2023-07-05 18:43  穿过雾的阴霾  阅读(10)  评论(0)    收藏  举报