使用HashSet,解决有环无环,相交不相交的情况
两个链表的第一个公共结点
http://www.nowcoder.com/questionTerminal/6ab1d9a29e88450685099d45c9e31e46
import java.util.HashSet;
public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
if(pHead1 == null || pHead1 == null){
return null;
}
HashSet<ListNode> set = new HashSet<>();
HashSet<ListNode> set_2 = new HashSet<>();
while(pHead1 != null && !set.contains(pHead1)){
set.add(pHead1);
pHead1 = pHead1.next;
}
while(pHead2 != null && !set_2.contains(pHead2)){
set_2.add(pHead2);
if(set.contains(pHead2)){
return pHead2;
}
pHead2 = pHead2.next;
}
return null;
}
}