Skip to content

Commit 782f3c9

Browse files
committed
GitBook: [master] 2 pages modified
1 parent c6a13ba commit 782f3c9

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [OF3.数组中重复的数字](docs/jzof/of003.md)
1010
* [OF4. 二维数组中的查找](docs/jzof/of004.md)
1111
* [OF5. 替换空格](docs/jzof/of005.md)
12+
* [OF6.从尾到头打印链表](docs/jzof/of006.md)
1213
* [OF37.序列化二叉树](docs/jzof/of037.md)
1314
* [OF14-I.剪绳子](docs/jzof/of014-i.md)
1415
* [OF14-II.剪绳子](docs/jzof/of014-ii.md)

docs/jzof/of006.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
description: 剑指 Offer 06. 从尾到头打印链表
3+
---
4+
5+
# OF6.从尾到头打印链表
6+
7+
## 题目描述
8+
9+
[题目地址](https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/)
10+
11+
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
12+
13+
### **示例 1:**
14+
15+
```go
16+
输入:head = [1,3,2]
17+
输出:[2,3,1]
18+
```
19+
20+
## 题解
21+
22+
### 思路1 : 递归
23+
24+
递归遍历
25+
26+
**算法流程:**
27+
28+
1.
29+
**复杂度分析:**
30+
31+
* **时间复杂度**$$O(N)$$****遍历N次,递归 N 次
32+
* **空间复杂度**$$O(N)$$****递归 N 次,开辟 N 个栈空间
33+
34+
#### 代码
35+
36+
{% tabs %}
37+
{% tab title="Go" %}
38+
```go
39+
func reversePrint(head *ListNode) []int {
40+
if head == nil {
41+
return []int{}
42+
}
43+
pre, cur, next, ans := &ListNode{}, head, head.Next, []int{}
44+
for cur != nil {
45+
next = cur.Next
46+
cur.Next = pre
47+
48+
pre = cur
49+
cur = next
50+
}
51+
for pre.Next != nil {
52+
ans = append(ans, pre.Val)
53+
pre = pre.Next
54+
}
55+
return ans
56+
}
57+
58+
```
59+
{% endtab %}
60+
{% endtabs %}
61+
62+
###
63+
64+
### 思路1 : 多指针
65+
66+
多个指针辅助,一次遍历
67+
68+
**算法流程:**
69+
70+
1.
71+
**复杂度分析:**
72+
73+
* **时间复杂度**$$O(N)$$****遍历N次,递归 N 次
74+
* **空间复杂度**$$O(N)$$****递归 N 次,开辟 N 个栈空间
75+
76+
#### 代码
77+
78+
{% tabs %}
79+
{% tab title="Go" %}
80+
```go
81+
func reversePrint(head *ListNode) []int {
82+
ans := make([]int, 0)
83+
if head == nil {
84+
return ans
85+
}
86+
ans = reversePrint(head.Next)
87+
ans = append(ans, head.Val)
88+
return ans
89+
}
90+
```
91+
{% endtab %}
92+
{% endtabs %}
93+
94+
## 总结
95+
96+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 算法 题解:[awesome-golang-algorithm](https://github.com/kylesliu/awesome-golang-algorithm)
97+

0 commit comments

Comments
 (0)