剑指OFFER----面试题52. 两个链表的第一个公共节点

链接:https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/

代码

/**
 * 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 (p) p = p->next;
            else  p = headB;
            if (q) q = q->next;
            else q = headA;       
        }
        return p;
    }
};
posted @ 2020-03-09 15:29  景云ⁿ  阅读(84)  评论(0编辑  收藏  举报