---
title: "How to determine if a given linked list contains a loop, and how would you remove the loop?"  
description: "How to determine if a given linked list contains a loop, and how would you remove the loop?"  
author: "Steilla Mitchel"  
published: 2023-06-13  
updated: 2023-06-16  
canonical: https://www.mindstick.com/forum/158730/how-to-determine-if-a-given-linked-list-contains-a-loop-and-how-would-you-remove-the-loop  
category: "data structure"  
tags: ["data structure"]  
reading_time: 2 minutes  

---

# How to determine if a given linked list contains a loop, and how would you remove the loop?

How to [determine if](https://www.mindstick.com/forum/158797/create-a-rust-program-to-determine-if-a-number-is-prime-or-not) a given [linked](https://answers.mindstick.com/qa/104773/how-to-create-a-linked-list) list contains a loop, and how would you [remove](https://yourviews.mindstick.com/story/4554/8-harmful-weeds-to-remove-from-garden) the loop?

## Replies

### Reply by Aryan Kumar

There are two common ways to determine if a given linked list contains a loop:

**Floyd's cycle detection algorithm:** This algorithm uses two pointers, one fast and one slow. The fast pointer moves two nodes at a time, while the slow pointer moves one node at a time. If the two pointers ever meet, then the linked list contains a loop.

**Using a hash table:** This algorithm stores the addresses of all the nodes in a hash table. If the same node is encountered twice, then the linked list contains a loop.

To remove the loop from a linked list, you can use the following steps:

**Find the starting node of the loop:** You can use either of the two methods mentioned above to find the starting node of the loop.

**Break the loop:** Once you have found the starting node of the loop, you can break it by setting the next pointer of the node to `null`.

Here is an example of how to determine if a linked list contains a loop and remove it in Python:

Python

```plaintext
def is_loop_exists(head):
    slow = head
    fast = head

    while slow and fast and fast.next:
        slow = slow.next
        fast = fast.next.next

        if slow == fast:
            return True

    return False

def remove_loop(head):
    slow = head
    fast = head

    while slow and fast and fast.next:
        slow = slow.next
        fast = fast.next.next

        if slow == fast:
            break

    while slow.next != fast.next:
        slow = slow.next
        fast = fast.next

    slow.next = None
```

Here is an explanation of the code:

- The `is_loop_exists()` function uses Floyd's cycle detection algorithm to determine if the linked list contains a loop.
- The `remove_loop()` function uses the following steps to remove the loop from the linked list:

   1. Find the starting node of the loop using the `is_loop_exists()` function.
   2. Set the next pointer of the starting node to `null`.


---

Original Source: https://www.mindstick.com/forum/158730/how-to-determine-if-a-given-linked-list-contains-a-loop-and-how-would-you-remove-the-loop

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
