题目描述 
一个链表中包含环,请找出该链表的环的入口结点。
/**
 * 链表中环的入口节点
 * 
 * @author 过路的守望
 *
 */
public class EntryNodeOfLoop {
    public ListNode getEntryNodeOfLoop(ListNode head) {
        if (head == null || head.next == null) {
            return null;
        }
        ListNode firstListNode = head;
        ListNode secondListNode = head;
        ListNode node = getNodeOfLoop(firstListNode, secondListNode);
        int len = getLengthOfLoop(node);
        //当第二个指针指向环的入口结点时,第一个指针已经围绕着环走了一圈又回到了入口结点。
        while (len > 0) {
            firstListNode = firstListNode.next;
            len--;
        }
        while (firstListNode != secondListNode) {
            firstListNode = firstListNode.next;
            secondListNode = secondListNode.next;
        }
        return firstListNode;
    }
    /**
     * 找环中相汇点。分别用first,second指向链表头部,first每次走一步,second每次走二步,直到first == second找到在环中的相汇点。
     * @param first
     * @param second
     * @return
     */
    public ListNode getNodeOfLoop(ListNode first, ListNode second) {
        first = first.next;
        while (first != second) {
            first = first.next.next;
            second = second.next;
        }
        return first;
    }
    /**
     * 求出环中节点数量
     * @param node
     * @return
     */
    public int getLengthOfLoop(ListNode node) {
        ListNode cur = node;
        int len = 1;
        while (cur.next != node) {
            cur = cur.next;
            len++;
        }
        return len;
    }
    /**
     * 
     * 链表类
     *
     */
    private static class ListNode {
        int val;
        ListNode next = null;
        ListNode(int val) {
            this.val = val;
        }
    }
}
                
                  
                  
                  
                  
                            
本文介绍了一种高效算法来确定链表中环的入口节点。通过双指针技巧找到环内相遇点,并计算环的长度,最终确定环的入口节点。
          
      
          
                
                
                
                
              
                
                
                
                
                
              
                
                
              
            
                  
					569
					
被折叠的  条评论
		 为什么被折叠?
		 
		 
		
    
  
    
  
            


            