题目描述:一个链表中包含环,请找出该链表的环的入口结点。

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 */
11 import java.util.HashMap;
12 import java.util.Map;
13 public class Solution {
14 
15     public ListNode EntryNodeOfLoop(ListNode pHead)
16     {
17              Map<ListNode,Integer>map=new HashMap<ListNode,Integer>();
18        while(pHead!=null){
19            if(map.containsKey(pHead)){
20                return pHead;
21            }else{
22                map.put(pHead,1);
23            }
24            pHead=pHead.next;
25        }
26        return null;
27     }
28 }

 

 posted on 2018-04-12 19:09  几缕清风依旧  阅读(130)  评论(0编辑  收藏  举报