注意: ListNode a1与b1的子节点
public class JavaDemoTest {
public static void main(String[] args) {
ListNode a1 = new ListNode(7);
ListNode b1 = new ListNode(6);
ListNode a2 = new ListNode(1);
ListNode a3 = new ListNode(3);
a1.next = a2;
a2.next = a3;
b1.next=a2;
System.out.println(FindFirstCommonNode(a1, b1));
}
public static ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
if (pHead1 != null && pHead2 != null) {
ListNode pHead2Temp = pHead2;
while (pHead1 != null) {
while (pHead2 != null) {
if (pHead1 == pHead2) {
return pHead1;
}
pHead2 = pHead2.next;
}
pHead2 =pHead2Temp;
pHead1 = pHead1.next;
}
}
return null;
}
}