package main import "fmt" type ListNode struct{ Val int Next *ListNode } func ReverseList(pHead *ListNode) *ListNode{ if(pHead == nil || pHead.Next == nil){ return pHead } var pre *ListNode = nil var cur *ListNode = pHead var tmp *ListNode = nil for cur != nil{ tmp = cur.Next cur.Next = pre pre = cur cur = tmp } return pre } func main(){ a := new(ListNode) a.Val = 1 b := new(ListNode) b.Val = 2 c := new(ListNode) c.Val = 3 d := new(ListNode) d.Val = 4 e := new(ListNode) e.Val = 5 a.Next = b b.Next = c c.Next = d d.Next = e pre_a := a for pre_a != nil{ fmt.Println(pre_a.Val) pre_a = pre_a.Next } fmt.Println("*****") a_reverse := ReverseList(a) pre_a_reverse := a_reverse for pre_a_reverse != nil{ fmt.Println(pre_a_reverse.Val) pre_a_reverse = pre_a_reverse.Next } }