美文网首页
找两个链表的公共结点

找两个链表的公共结点

作者: yeying12321 | 来源:发表于2017-12-10 16:55 被阅读8次
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