标签:oop node == 一个 import set nod turn bsp
一个链表中包含环,请找出该链表的环的入口结点。
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.HashSet; 12 public class Solution { 13 public ListNode EntryNodeOfLoop(ListNode pHead){ 14 HashSet<ListNode> hs = new HashSet<ListNode>(); 15 while(pHead!=null){ 16 if(!hs.add(pHead)) 17 return pHead; 18 pHead = pHead.next; 19 } 20 return null; 21 } 22 /* public ListNode EntryNodeOfLoop(ListNode pHead) 23 { 24 if(pHead == null || pHead.next == null) 25 return null; 26 ListNode p1 = pHead; 27 ListNode p2 = pHead; 28 while(p2 != null && p2.next != null){ 29 p1 = p1.next; 30 p2 = p2.next.next; 31 if(p1 == p2){ 32 p2 = pHead; 33 while(p1 != p2){ 34 p1 = p1.next; 35 p2 = p2.next; 36 } 37 if(p1 == p2) 38 return p1; 39 } 40 } 41 return null; 42 }*/ 43 }
标签:oop node == 一个 import set nod turn bsp
原文地址:http://www.cnblogs.com/fankongkong/p/7460243.html