随笔- 107  文章- 0  评论- 6  阅读- 96308 

题目描述:输入两个链表,找出它们的第一个公共结点。

ac代码:

复制代码
 1 /*
 2 public class ListNode {
 3     int val;
 4     ListNode next = null;
 5 
 6     ListNode(int val) {
 7         this.val = val;
 8     }
 9 }*/
10 import java.util.HashMap;
11 public class Solution {
12     public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
13          ListNode p=pHead1;
14         ListNode q=pHead2;
15         HashMap<ListNode,Integer>list=new HashMap<ListNode,Integer>();
16         while(p!=null){
17             list.put(p,1);
18             p=p.next;
19         }
20         while(q!=null){
21             if(list.containsKey(q)){
22                 break;
23             }
24             q=q.next;
25                 
26         }
27         return q;
28         
29     }
30 }
复制代码

 

 posted on   几缕清风依旧  阅读(132)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示