Niuke classic linked list question—(NC3) the entry node of the ring in the linked list

Full difficulty factor * * * * *, the difficulty factor of this question * * *.
Frequent enthusiasm for full exams, enthusiasm * * * * *for this question * * * * *.

1. Title description

Insert picture description here

2. Topic link

  1. The entry node of the ring in the link list of the cattle problem

3. Problem analysis

  1. First judge whether there is a ring in the linked list, if not, there will be no entry node, and directly return null. Is there a link?
  2. If there are loops in the linked list, nullptr will not be reached during traversal, and a fast pointer and a slow pointer are defined. 当fast和slow进环以就会围着环同时运动,且fast比slow每次多走一步。直到fast和slow相遇.
  3. The point must be met inside the ring, and then define a count, in the definition of a node RmNodefrom the next node node encounter, when every step count ++, and then come to this node count就是这个环里面有多少个结点.
  4. Redefinition two nodes Rnode1, Rnode2 start from scratch, 让Rnode1先走count步.
  5. 再让Rnode1,Rnode2同时走, The meeting point is the entry node of the ring.

3.1 Analysis icon

Insert picture description here
Insert picture description here
Insert picture description here

3.2 Detailed explanation of icons

  1. Let the fast and slow pointers meet in the ring first. 图一.
  2. Starting from the next point of the encounter, calculate how many nodes there are in the ring. 图二.
  3. Define two nodes to start from the beginning, let one node go the number of nodes in the ring first, and then the two rings go at the same time. 图三.

4. Problem-solving code

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode *detectCycle(ListNode *head) {
    
    
        //注释1
        if(head == nullptr || head->next == nullptr)
        {
    
    
            return nullptr;
        }
        //注释2
        ListNode *fast = head;
        ListNode *slow = head;
        ListNode *point = nullptr;
        int count = 1;
        ListNode *RmNode =  nullptr;
        
        while(fast && fast->next)
        {
    
    
            fast = fast -> next -> next;
            slow = slow -> next;
            if(fast == slow)
            {
    
    
                point = fast;
                break;
            }
        }
        //注释3
        if(point == nullptr)
        {
    
    
            return nullptr;
        }
        //注释4
        RmNode = point->next;
        while(RmNode != point)
        {
    
    
            count++;
            RmNode = RmNode -> next;
        }
        //注释5
        ListNode *Rnode1 = head;
        ListNode *Rnode2 = head;
        while(count)
        {
    
    
            Rnode1 = Rnode1 -> next;
            count--;
        }
        //注释6
        while(1)
        {
    
    
            if(Rnode1 == Rnode2)
            {
    
    
                return Rnode2;
            }
            Rnode1 = Rnode1 -> next;
            Rnode2 = Rnode2 -> next;
        }
        
        return nullptr;
    }
};

5. Detailed explanation of code comments

  1. Note 1: The head pointer is nullified first. So as not to cause null pointer access.
  2. Note 2: Define fast and slow pointers and let them meet in the ring.
  3. Note 3: If point == nullptr, it means there is no ring in the linked list, and nullptr is returned.
  4. Note 4: Let the RmNode node start from point->next (the next node where the fast and slow pointers meet) and go backward, and then use count to count the number of nodes in the ring.
  5. Note 5: Let Rnode1 go back count steps from the beginning.
  6. Note 6: Let Rnode1 and Rnode2 go backward at the same time, the meeting point is the entry node of the ring.

The algorithm complexity of finding the entry node is:

  • Time complexity: O(n)
  • Space complexity: O(1)

1.如有错误或者没能理解的地方,请及时评论或者私信,及时修改更新
2.会持续更新相关链表高频题目,分类专栏—数据结构

Guess you like

Origin blog.csdn.net/weixin_45313447/article/details/115324679