type LinkNode struct {
data int
next *LinkNode
}
func reverseNode(node *LinkNode) {
if node == nil {
return
}
result := list.New()
for i := node; i != nil; i = node.next {
fmt.Println(i.data)
result.PushFront(i.data)
}
//反向遍历
for v := result.Back(); v != nil; v = v.Prev() {
fmt.Println(v.Value)
}
}
func main() {
node := LinkNode{data: 1}
next := LinkNode{data: 2}
node.next = &next
reverseNode(&node)
}
第一个`for`循环中`i = node.next` 替换为 `i = i.next`
```go
package main
import (
"container/list"
"fmt"
)
type LinkNode struct {
data int
next *LinkNode
}
func reverseNode(node *LinkNode) {
if node == nil {
return
}
result := list.New()
for i := node; i != nil; i = i.next {
fmt.Println(i.data)
result.PushFront(i.data)
}
//反向遍历
for v := result.Back(); v != nil; v = v.Prev() {
fmt.Println(v.Value)
}
}
func main() {
node := LinkNode{data: 1}
next := LinkNode{data: 2}
node.next = &next
reverseNode(&node)
}
```
#1
更多评论
楼上回的是对的,突然想起了我当时造的小轮子 https://github.com/Juntaran/Golang_Algorithm/blob/master/Data_Structure/Link_List/link_list.go
#2