Skip to content

Instantly share code, notes, and snippets.

@GabrielModog
Created January 22, 2025 08:09
Show Gist options
  • Save GabrielModog/e0e4d74c0fe31be2c0c08ea566db10dd to your computer and use it in GitHub Desktop.
Save GabrielModog/e0e4d74c0fe31be2c0c08ea566db10dd to your computer and use it in GitHub Desktop.

Revisions

  1. GabrielModog created this gist Jan 22, 2025.
    38 changes: 38 additions & 0 deletions cycle-nodes.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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))
    }