📜  使用 Map 检测链表中的循环

📅  最后修改于: 2021-09-07 05:09:46             🧑  作者: Mango

给定一个链表,检查链表是否有循环。
这里显示了各种方法:检测链表中的循环

例子

方法:

  1. 创建一个映射,将访问的节点存储在链表中。
  2. 遍历链表并执行以下操作:
    • 检查当前节点是否存在于地图上。
    • 如果当前节点不存在于地图中,则将当前节点插入到地图中。
    • 如果节点存在于地图中,则检测到链表中的循环。
  3. 如果我们在遍历链表时到达空节点,则给定的链表中没有循环。

下面是上述方法的实现:

C++
// C++ program to detect loop in
// given linked list using map
#include 
using namespace std;
 
// Structure for a node in Linked List
struct Node {
    int data;
    Node* next;
};
 
// Function to create Linked List
// Node
Node* newNode(int d)
{
    Node* temp = new Node;
    temp->data = d;
    temp->next = NULL;
    return temp;
}
 
// Declaration of Map to keep
// mark of visited Node
map vis;
bool flag = 0;
 
// Function to check cycle in Linked
// List
void check(Node* head)
{
    // If head is NULL return ;
    if (head == NULL) {
        flag = 0;
        return;
    }
 
    // Mark the incoming Node as
    // visited if it is not visited yet
    if (!vis[head]) {
        vis[head] = true;
        check(head->next);
    }
 
    // If a visited Node is found
    // Update the flag value to 1
    // and return ;
    else {
        flag = 1;
        return;
    }
}
 
// Driver Code
int main()
{
    // Create a head Node
    Node* head = newNode(20);
 
    // Inserting Node in Linked List
    head->next = newNode(4);
    head->next->next = newNode(5);
    head->next->next->next = newNode(10);
 
    // Just to make a cycle
    head->next->next->next->next = head;
 
    // Function that detect cycle in
    // Linked List
    check(head);
 
    // If flag is true, loop is found
    if (flag)
        cout << "Loop detected.";
 
    // If flag is false, No Loop
    // detected
    else
        cout << "No Loop Found.";
    cout << endl;
 
    return 0;
}


Java
// Java program to detect loop in
// given linked list using map
import java.util.*;
 
class GFG{
 
// Structure for a node in Linked List
static class Node
{
    int data;
    Node next;
};
 
// Function to create Linked List
// Node
static Node newNode(int d)
{
    Node temp = new Node();
    temp.data = d;
    temp.next = null;
    return temp;
}
 
// Declaration of Map to keep
// mark of visited Node
static HashMap vis = new HashMap<>();
static boolean flag = false;
 
// Function to check cycle in Linked
// List
static void check(Node head)
{
     
    // If head is null return ;
    if (head == null)
    {
        flag = false;
        return;
    }
 
    // Mark the incoming Node as
    // visited if it is not visited yet
    if (vis.containsKey(head))
    {
        vis.put(head, true);
        check(head.next);
    }
 
    // If a visited Node is found
    // Update the flag value to 1
    // and return ;
    else
    {
        flag = true;
        return;
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Create a head Node
    Node head = newNode(20);
 
    // Inserting Node in Linked List
    head.next = newNode(4);
    head.next.next = newNode(5);
    head.next.next.next = newNode(10);
 
    // Just to make a cycle
    head.next.next.next.next = head;
 
    // Function that detect cycle in
    // Linked List
    check(head);
 
    // If flag is true, loop is found
    if (flag)
        System.out.print("Loop detected.");
 
    // If flag is false, No Loop
    // detected
    else
        System.out.print("No Loop Found.");
         
    System.out.println();
}
}
 
// This code is contributed by Rajput-Ji


C#
// C# program to detect loop in
// given linked list using map
using System;
using System.Collections.Generic;
 
class GFG{
 
// Structure for a node in Linked List
public class Node
{
    public int data;
    public Node next;
};
 
// Function to create Linked List
// Node
static Node newNode(int d)
{
    Node temp = new Node();
    temp.data = d;
    temp.next = null;
    return temp;
}
 
// Declaration of Map to keep
// mark of visited Node
static Dictionary vis = new Dictionary();
static bool flag = false;
 
// Function to check cycle in Linked
// List
static void check(Node head)
{
     
    // If head is null return ;
    if (head == null)
    {
        flag = false;
        return;
    }
 
    // Mark the incoming Node as
    // visited if it is not visited yet
    if (vis.ContainsKey(head))
    {
        vis.Add(head, true);
        check(head.next);
    }
 
    // If a visited Node is found
    // Update the flag value to 1
    // and return ;
    else
    {
        flag = true;
        return;
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Create a head Node
    Node head = newNode(20);
     
    // Inserting Node in Linked List
    head.next = newNode(4);
    head.next.next = newNode(5);
    head.next.next.next = newNode(10);
     
    // Just to make a cycle
    head.next.next.next.next = head;
 
    // Function that detect cycle in
    // Linked List
    check(head);
 
    // If flag is true, loop is found
    if (flag)
        Console.Write("Loop detected.");
 
    // If flag is false, No Loop
    // detected
    else
        Console.Write("No Loop Found.");
         
    Console.WriteLine();
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
Loop detected.

时间复杂度: O(N*log N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live