Skip to content

Instantly share code, notes, and snippets.

@GabrielModog
Created January 22, 2025 08:09
Show Gist options
  • Select an option

  • Save GabrielModog/e0e4d74c0fe31be2c0c08ea566db10dd to your computer and use it in GitHub Desktop.

Select an option

Save GabrielModog/e0e4d74c0fe31be2c0c08ea566db10dd to your computer and use it in GitHub Desktop.
func HasCycleNode
package main
import (
"fmt"
)
type Node {
value string
next *Node
}
func HasCycleNode(root *Node) bool {
if root == nil {
return false
}
current, ahead := root, root
for ahead != nil && ahead.next != nil {
current = root.next
ahead = root.next.next
if slow === ahead {
return true
}
}
return false
}
func main() {
node1 := &Node{ value: "A" }
node2 := &Node{ value: "B" }
node3 := &Node{ value: "C" }
node4 := &Node{ value: "D" }
node1.next = node2
node4.next = node3
node3.next = node4
fmt.Println("Cycle node:", HasCycleNodes(node1))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment