Created
January 22, 2025 08:09
-
-
Save GabrielModog/e0e4d74c0fe31be2c0c08ea566db10dd to your computer and use it in GitHub Desktop.
func HasCycleNode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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