/*
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;
}
ListNode nodeL = pHead1;
ListNode nodeS = pHead2;
int L1 = length(pHead1);
int L2 = length(pHead2);
int L = L1 - L2;
if(L<0){
nodeL=pHead2;
nodeS=pHead1;
L=-L;
}
while(L>0){
L--;
nodeL=nodeL.next;
}
while(nodeL!=nodeS){
nodeL=nodeL.next;
nodeS=nodeS.next;
}
if(nodeL==null){
return null;
}
return nodeL;
}
public static int length(ListNode head){
ListNode node = head;
int length=0;
while(node!=null){
length++;
node=node.next;
}
return length;
}
}