面试题06. 从尾到头打印链表

package mainimport "fmt"type ListNode struct {Val intNext *ListNode}func reversePrint(head *ListNode) []int {stack :=make([]*ListNode,0)for head!=nil{stack = append(stack,head)head =head.Next}var res []intfor len(stack)>0{node :=stack[len(stack)-1]res = append(res,node.Val)stack = stack[:len(stack)-1]}return res}func main() {one :=&ListNode{Val:1}three :=&ListNode{Val:3}one.Next = threetwo :=&ListNode{Val:2}three.Next=twofmt.Println(reversePrint(one))}

