前言
记录自己刷题思路,时常复习。
一、链表
二、解题思路
1.剑指 Offer 06. 从尾到头打印链表
题目要求返回一个数组,则初始化一个数组res[] ,用来存放结果
利用递归思想,递归停止条件为当前节点head.next为空;则res插入head.val
当前节点head.next不为空时,执行递归
res插入head.val
go代码如下:
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reversePrint(head *ListNode) []int {
if head == nil {
return nil
}
var res []int
if head.Next == nil {
res = append(res, head.Val)
return res
}
res = reversePrint(head.Next)
res = append(res, head.Val)
return res
}
总结
还是用到了栈的思想,函数递归也是栈的思想。