Leetcode 160 Solution

This article provides solution to leetcode question 160 (intersection-of-two-linked-lists)

https://leetcode.com/problems/intersection-of-two-linked-lists

Solution

/**
 * 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) {
        if (headA == NULL || headB == NULL)
            return NULL;

        auto p1 = headA;
        auto p2 = headB;

        while (p1 || p2)
        {
            if (p1 == NULL)
                p1 = headB;
            if (p2 == NULL)
                p2 = headA;

            if (p1 == p2)
                return p1;

            p1 = p1->next;
            p2 = p2->next;
        }

        return NULL;
    }
};