import java.util.*;
/*
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 || pHead2 == null) return null;
if(pHead1 == pHead2) return pHead1;
List<ListNode> nodeList1 = new ArrayList<>();
ListNode temp1 = pHead1;
nodeList1.add(temp1);
while (true) {
ListNode next = temp1.next;
if (next == null) break;
nodeList1.add(next);
temp1 = next;
}
List<ListNode> nodeList2 = new ArrayList<>();
ListNode temp2 = pHead2;
nodeList2.add(temp2);
while (true) {
ListNode next = temp2.next;
if (next == null) break;
nodeList2.add(next);
temp2 = next;
}
ListNode target = null;
for(int i=0; i<nodeList1.size(); i++){
if(nodeList2.contains(nodeList1.get(i))){
target = nodeList1.get(i);
break;
}
}
return target;
}
}