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

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

}```

```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() {