# Implementation of Floyd’s Cycle-Finding Algorithm in Golang

## What is a loop in a linked list

A very common operation in a linked list is to traverse throughout the LinkedList. But when no null value is reached as traversing throughout the linked list, we call this as loops in a linked list. So to detect whether a LinkedList has a loop or not, we can use Floyd’s Cycle-Finding Algorithm also known as a slow-fast pointer or hear tortoise algorithm.

We are implementing Floyd’s Cycle-Finding Algorithm to find loops in a linked list

### Node & Linked list struct

```type Node struct {
data interface{}
next *Node
}

}```

### Push to a Linked List

```func (ll *LinkedList) Push(data interface{}) {
list := &Node{
data: data,
}
}

for l.next != nil {
l = l.next
}
}```

```func (ll *LinkedList) Display() {
for list != nil {
fmt.Printf("%+v -> ", list.data)
list = list.next
}
fmt.Println()
}```

### Detect loops in LinkedList using Floyd’s Cycle-Finding Algorithm

```func (ll *LinkedList) DetectLoop() bool {

for slowPtr != nil && fastPtr != nil && fastPtr.next != nil {
slowPtr = slowPtr.next
fastPtr = fastPtr.next.next
if slowPtr == fastPtr {
return true
}
}
return false
}```

If you like, you can read the same article on my Personal Blog

### Main function

```func main() {

// creating a loop in the above linked list

fmt.Println("found loop")
} else {
fmt.Println("no loop")
}
}

// output
found loop```
By Vivasoft Team
##### Catagories
Hire Exceptional Developers Quickly

## Hire a Talented Developer from Vivasoft

Lets discuss about your offshore project. We can Help you by our skillful Global team. You can take a free counciling by taking a schedule

## Related Post

### How to Deploy Python Flask Web App on Azure

Deploy your Flask web app with unparalleled ease, thanks to Azure App Service. Whether you’re looking to deploy your Flask project or deploy a Python-based