劍指offer-鏈表中環的入口結點
阿新 • • 發佈:2018-04-12
bsp ins val ont 入口 ger span key java
題目描述:一個鏈表中包含環,請找出該鏈表的環的入口結點。
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 }
劍指offer-鏈表中環的入口結點