package main
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
// 创建一个虚拟头节点
func reverseList(head *ListNode) *ListNode {
// 创建一个虚拟头节点
k := &ListNode{}
k.Next = nil
for head != nil {
p := head
head = head.Next
p.Next = k.Next
k.Next = p
}
return k.Next
}
func main() {
var head ListNode
head.Val = 100
// 尾部插入节点
var tail = &head
for i := 0; i < 10; i++{
var node = ListNode{
Val: i,
}
tail.Next = &node
tail = &node
}
// 打印链表
trans(&head)
// 反转链表
reverseNode := reverseList(&head)
// 打印反转后的链表
fmt.Println("=======================")
trans(reverseNode)
}
func trans(head *ListNode) {
for head != nil {
fmt.Println(*head)
head = head.Next
}
}