找两个链表的公共结点
public class ListNode {
int val;
ListNode next=null;
ListNode(int val){
this.val=val;
}
}
public class Solution {
public ListNode findFirstCommonNode(ListNode pHead1,ListNode pHead2){
ListNode cur1=pHead1;
ListNode cur2=pHead2;
HashMap<ListNode,Integer> hashMap=new HashMap<>();
while (cur1!=null){
hashMap.put(cur1,null);
cur1=cur1.next;
}
while (cur2!=null){
if(hashMap.containsKey(cur2)){
return cur2;
}
cur2=cur2.next;
}
return null;
}
}
本文标题:找两个链表的公共结点
本文链接:https://www.haomeiwen.com/subject/ixsvixtx.html
网友评论