Skip to content

Instantly share code, notes, and snippets.

@ympons
Created March 14, 2016 03:07
Show Gist options
  • Save ympons/3e0cdf7ce29674ad5e68 to your computer and use it in GitHub Desktop.
Save ympons/3e0cdf7ce29674ad5e68 to your computer and use it in GitHub Desktop.
Reverse Single Linked List
//@ympons - https://play.golang.org/p/mNEabcyTVa
package main
import "fmt"
type Node struct {
data int
next *Node
}
func Reverse(head **Node, p *Node){
if (p.next == nil) {
*head = p
return
}
Reverse(head, p.next)
p.next.next = p
p.next = nil
}
// 1 -> 2 -> 3 -> nil
func Print(head *Node) {
current := head
for(current != nil) {
fmt.Println(current .data)
current = current .next
}
}
func main() {
head := &Node{1, &Node{2, &Node{3, nil}}}
Print(head)
fmt.Println("Reverse")
Reverse(&head, head)
Print(head)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment