package main
import (
"fmt"
)
type node struct {
value int
next *node
}
func newNode(arr []int) (tmpnode *node) {
tmpnode = new(node)
if len(arr) > 1 {
tmpnode.value = arr[0]
tmpnode.next = newNode(arr[1:])
} else {
if len(arr) == 1 {
tmpnode.value = arr[0]
}
}
return
}
func swap(node *node) *node {
if node == nil {
return node
}
tmp := node
if node.next != nil {
node = node.next
tmp.next, tmp.next.next = swap(tmp.next.next), tmp
}
return node
}
func main() {
arr := []int{1, 2, 3, 4, 5}
chart := newNode(arr[:])
swapedChart := swap(chart)
fmt.Println(swapedChart.next.next.next.next)
}
例如 1 2 3 4 , 1 和 2交换, 3 和 4 交换 返回 [2,1,4,3]