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

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
      /*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
import java.util.*;
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        
       /* if(pHead2 == null || pHead1 == null){
            return null;
        }
        ListNode p1 = pHead2;
        ListNode p2;
        for(; pHead1 !=null;pHead1=pHead1.next){
            pHead2 = p1;
            for(;pHead2 != null;pHead2=pHead2.next){
             
                if(pHead1 == pHead2){
                    return pHead1;
                }
            }
        }
        return null;*/
        /*if(pHead2 == null || pHead1 == null){
            return null;
        }
        Set<ListNode> we = new HashSet<ListNode>();
        for(;pHead1 != null;pHead1=pHead1.next){
            we.add(pHead1);
        }
        for(;pHead2 != null;pHead2=pHead2.next){
            if(we.contains(pHead2)){
                return pHead2;
            }
        }
        return null;*/
        //大佬思路牛皮,自己的太low了
       
        if(pHead2 == null || pHead1 == null){
            return null;
        }
        ListNode p1 = pHead2;
        ListNode p2;
        for(; pHead1 !=null;pHead1=pHead1.next){
            pHead2 = p1;
            for(;pHead2 != null;pHead2=pHead2.next){
             
                if(pHead1 == pHead2){
                    return pHead1;
                }
            }
        }
        return null;
    }
}