题目描述
输入两个链表,找出它们的第一个公共结点。(注意因为传入数据是链表,所以错误测试数据的提示是用其他方式显示的,保证传入数据是正确的)
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
import java.util.HashMap;
public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
ListNode c1 = pHead1;
ListNode c2 =pHead2;
HashMap<ListNode,Integer> h1 = new HashMap<ListNode, Integer>();
while (c1 != null) {
h1.put(c1, null);
c1 = c1.next;
}
while(c2!=null){
if(h1.containsKey(c2)){
return c2;
}
c2=c2.next;
}
return null;
}
}