【发布时间】:2021-05-18 11:19:18
【问题描述】:
当我从 Leetcode 解决问题Intersection of Two Linked Lists 时,我发现 HashSet 解决方案在我的计算机上不起作用。 返回结果始终为 null,但在 Leetcode 上运行良好。
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Set<ListNode> hashSet = new HashSet<ListNode>();
ListNode curNode = headA;
while (curNode!=null){
hashSet.add(curNode);
curNode=curNode.next;
}
curNode=headB;
while (curNode!=null){
if (!hashSet.add(curNode)){
return curNode;
}
curNode=curNode.next;
}
return null;
}
【问题讨论】: