Related Articles
Detect Cycle in a Linked List using Map
• Last Updated : 18 Jan, 2021

Given a Linked List, check if the linked list has a loop or not.
There are various methods shown here: Detect Cycle in Linked List

Example

Input: 20->4->54->6->NULL
Output: No loop is detected.
Explanation:
While traversing the linked list we reached the end of linked list. Therefore no loop is present in linked list.

Input: 20->4->5->10->20
Output: Loop detected.
Explanation:
While traversing the linked list we reached the node with value 10 is linked with the head node which depicts a loop in the linked list. Therefore loop is present in linked list.

Approach:

1. Create a map which will store the visited node in linked list.
2. Traverse the linked list and do the following:
• Check whether the current node is present in the map or not.
• If the current node is not present in the map then, insert the current node in the map.
• If the Node is present in the map, the loop in a linked list is detected.
3. If we reached to Null Node while traversing the linked list then, the given linked list has no loop present in it.

Below is the implementation of above approach:

## 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`

Output:

`Loop detected.`

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :