"Finger offer" - the entry node of the chain central ring

Posted by olsrey on Sat, 05 Oct 2019 16:29:28 +0200

The entry node of the ring in the linked list

For a linked list, if it contains rings, please find the entry node of the ring of the linked list, otherwise, output null.
  1. Identify a link in a list: set two pointers to start from scratch, one fast and one slow. If they meet before meeting null, it means that there is a link in the list. And the nodes that meet are in the ring.
  2. How to Determine the Length of the Ring: Starting from the meeting node, set a pointer to traverse backwards, while traversing while counting, if you return to the original position, you can get the number of nodes in the Ring.
  3. How to determine the entry of the ring: now we have got the length of the ring, assuming that the total length of the linked list is n. This n value does not have to be asked, only to illustrate the convenience. The length before the ring is a and the length of the ring is b. Two nodes are set up, one node starts from scratch, at this time the entry node of the distance ring is a; a node starts from the node with a b distance from the head node, then the distance from the tail node is n - b - 1. After the tail node is the entry node of the ring, the distance is n - b, that is a. When two nodes meet, the node is the entry node of the ring.
/*
public class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ListNode EntryNodeOfLoop(ListNode pHead){
        if(pHead == null || pHead.next == null){
            return null;
        }
        ListNode pMiddle = meetingNode(pHead);
        if(pMiddle == null){
            return null;
        }
        int numOfList = count(pMiddle);
        if(numOfList == 0){
            return null;
        }
        ListNode pNode1 = pHead;
        ListNode pNode2 = pHead;
        for(int i=0;i<numOfList;i++){
            pNode1 = pNode1.next;
        }
        while(pNode1 != pNode2){
            pNode1 = pNode1.next;
            pNode2 = pNode2.next;
        }
        return pNode1;
    }
    // A node in the input ring, counting the length of the ring
    public int count(ListNode pNode){
        if(pNode == null || pNode.next == null){
            return 0;
        }
        ListNode pCount = pNode.next;
        int num = 1;
        while(pNode != pCount){
            if(pCount.next == null){
                return 0;
            }
            pCount = pCount.next;
            num++;
        }
        return num;
    }
    // Return any node in the ring
    public ListNode meetingNode(ListNode pHead){
        if(pHead == null || pHead.next == null){
            return null;
        }
        ListNode pFast = pHead;
        ListNode pLow = pHead;
        while(pFast.next != null){
            pFast = pFast.next;
            if(pFast.next != null){
                pFast = pFast.next;
            }else {
                return null;
            }
            pLow = pLow.next;
            if(pFast == pLow){
                return pFast;
            }
        }
        return null;
    }
}