I'm learning Go and have written the following code to reverse a linked list. However, the code doesn't work as expected.
Here's a Node structure along with the function for printing and reversing the list.
type Node struct {
number int
previous *Node
next *Node
}
func PrintList(node *Node) {
for n := node; n != nil; n = n.next {
fmt.Println(n)
}
}
func ReverseList(node *Node) {
var nextNodeRef *Node
for n := node; n != nil; n = n.previous {
if n.next == nil {
n.next = n.previous
n.previous = nil
*node = *n
break
} else {
nextNodeRef = n.next
n.next = n.previous
n.previous = nextNodeRef
if n.next == nil {
node = n
}
}
}
}
The problem is that when I reverse the list and call PrintList
I get a seemingly infinite output of all the elements of the list except for the last one (which used to be the first element)
Here's my main
function:
func main() {
myList := Node{1, nil, nil}
myListAddress := &myList
AddNumber(2, myListAddress)
AddNumber(3, myListAddress)
fmt.Println("My list:")
PrintList(myListAddress)
ReverseList(myListAddress)
fmt.Println("My list reversed:")
// here I always get list elements that contain 3, 2, 3, 2...
PrintList(myListAddress)
}
And here's the AddNumber
function that I use:
func AddNumber(number int, node *Node) *Node {
if node == nil {
log.Fatal("No Node provided")
}
var newNode Node
for n := node; n != nil; n = n.next {
if n.next == nil {
newNode = Node{number, n, nil}
n.next = &newNode
break
}
}
return &newNode
}