[leetcode] Determine whether the linked list has loops and its variants

Given a linked list, determine whether there is a ring in the linked list.

The requirements of the problem are as follows:
if there is a node in the linked list, it can be reached again by continuously tracking the next pointer, then there is a ring in the linked list. In order to represent the rings in a given linked list, we use the integer pos to indicate the position where the end of the linked list is connected to the linked list (the index starts from 0). If pos is -1, there are no rings in the linked list. Note: pos is not passed as a parameter, just to identify the actual situation of the linked list.

If there is a ring in the linked list, return true. Otherwise, it returns false.
As shown in the figure:
Example 1.
[leetcode] Determine whether the linked list has loops and its variants
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a ring in the linked list, and its tail is connected to the second node.

Example 2:
[leetcode] Determine whether the linked list has loops and its variants
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a ring in the linked list, and its tail is connected to the first node.
Example 3:
[leetcode] Determine whether the linked list has loops and its variants
Input: head = [1], pos = -1
Output: false
Explanation: There is no ring in the linked list.

First of all, we analyze the meaning of the question. This question needs to judge whether there is a ring in a given linked list as he requested, that is, when a node's next field points to a previous node, it forms a ring. Here I use the fast and slow pointer method to solve.
The fast pointer walks two steps at a time, and the slow pointer walks one step at a time. If the linked list itself has a ring, then the fast pointer will always be in this ring. When the fast pointer and the slow pointer point to the same node, it means that the linked list has a ring and returns true. , If it does not return until the end of the loop, indicating that the linked list has no loops, return false. The judgment condition for the end of the loop here is to fast && fast->next != NULL
solve the problem as follows:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head) {
    if(head == NULL)
        return NULL;
    struct ListNode *fast, *slow;
    fast = slow = head;
    while(fast && fast->next != NULL)
    {
        fast = fast->next->next;
        slow = slow->next;
        if(fast == slow)
            return true;
    }
    return false;
}

Variant: Given a linked list, return the first node where the linked list starts to enter the loop. If the linked list has no rings, null is returned.

In order to represent the rings in a given linked list, we use the integer pos to indicate the position where the end of the linked list is connected to the linked list (the index starts from 0). If pos is -1, there are no rings in the linked list. Note that pos is only used to identify the ring, and will not be passed to the function as a parameter.

Note: It is not allowed to modify the given linked list.
[leetcode] Determine whether the linked list has loops and its variants
Input: head = [3,2,0,-4], pos = 1
Output: Return the linked list node with index 1
Explanation: There is a ring in the linked list, and its tail is connected to the second node.
[leetcode] Determine whether the linked list has loops and its variants
Input: head = [1,2], pos = 0
Output: Return the linked list node with index 0
Explanation: There is a ring in the linked list, and its tail is connected to the first node.
[leetcode] Determine whether the linked list has loops and its variants
Input: head = [1], pos = -1
Output: return null
Explanation: There is no ring in the linked list.

This problem is a variant of judging that the linked list has a ring. What is required here is to return a ring node. The core here is to first judge whether the linked list has a ring, and then traverse the linked list to find the node to be returned.
The problem-solving process is as follows:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) {
    if(head == NULL || head->next == NULL)
        return NULL;
    struct ListNode *fast, *slow;
    fast = slow = head;
    while(fast && fast->next != NULL)
    {
        fast = fast->next->next;
        slow = slow->next;
        if(fast == slow)
            break;
    }
    if(fast != slow)
        return NULL;
    fast = head;
    while(fast != slow)
    {
        fast = fast->next;
        slow = slow->next;
    }
    return fast;
}

Guess you like

Origin blog.51cto.com/14289099/2544816