Created
March 14, 2016 03:07
-
-
Save ympons/3e0cdf7ce29674ad5e68 to your computer and use it in GitHub Desktop.
Reverse Single Linked List
This file contains 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
//@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