/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        if(pHead1 == null) {
            return null;
        }
        if(pHead2 == null) {
            return null;
        }
        if(pHead1 == pHead2) {
            return pHead1;
        }
        int offset = 1000000;
        int maxVal = 1000;
        ListNode p = pHead1;
        while(p != null) {
            p.val += offset;
            p = p.next;
        }
        p = pHead2;
        while(p != null) {
            if(p.val > maxVal) {
                ListNode q = p;
                while(q != null) {
                    q.val -= offset;
                    q = q.next;
                }
                return p;
            }
            p = p.next;
        }
        return null;
    }
}