Skip to content

Instantly share code, notes, and snippets.

@infoverload
Created November 19, 2019 20:26
Show Gist options
  • Save infoverload/9007763967e290435fe8cceb60627371 to your computer and use it in GitHub Desktop.
Save infoverload/9007763967e290435fe8cceb60627371 to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Node struct {
Data int
Next *Node
}
// deduplicates a linked list
func dedupsWithStore(node *Node) {
seenValues := map[int]bool{}
// loop through nodes while there is still a next
for n := node; n.Next != nil; n = n.Next {
if ok := seenValues[n.Next.Data]; !ok {
seenValues[n.Next.Data] = true
} else {
fmt.Printf("duplicate value: %d\n", n.Next.Data)
n.Next = n.Next.Next
}
fmt.Printf("value: %d\n", n.Data)
}
}
func main() {
n1 := Node{Data: 5}
n2 := Node{Data: 4, Next: &n1}
n3 := Node{Data: 5, Next: &n2}
n4 := Node{Data: 3, Next: &n3}
n5 := Node{Data: 2, Next: &n4}
dedupsWithStore(&n5)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment