两个链表的第一个公共节点
题目:输入两个链表,找出它们的第一个公共结点。
分析:用hashMap
1 public class Solution { 2 public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) { 3 HashMap<ListNode, Integer> hashMap = new HashMap<ListNode, Integer>(); 4 5 while(pHead1!=null){ 6 hashMap.put(pHead1,null); 7 pHead1=pHead1.next; 8 } 9 10 while(pHead2!=null){ 11 if(hashMap.containsKey(pHead2)){ 12 return pHead2; 13 } 14 pHead2=pHead2.next; 15 } 16 return null; 17 } 18 }

浙公网安备 33010602011771号