https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
1. 数组头插法
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reversePrint(head *ListNode) []int {
res := make([]int, 0)
for head != nil {
res = append([]int{head.Val}, res...)
head = head.Next
}
return res
}
2. 递归
func reversePrint(head *ListNode) []int {
res := make([]int, 0)
do(head, &res)
return res
}
func do(head *ListNode, res *[]int) {
if head == nil {
return
}
do(head.Next, res)
*res = append(*res, head.Val)
}