Skip to content

Commit 1e42fcc

Browse files
committed
Merge branch 'master' of github.com:kylesliu/awesome-golang-algorithm into master
2 parents aa2b7c6 + e679991 commit 1e42fcc

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

docs/jzof/of006.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,14 @@ description: 剑指 Offer 06. 从尾到头打印链表
3737
{% tab title="Go" %}
3838
```go
3939
func reversePrint(head *ListNode) []int {
40+
ans := make([]int, 0)
4041
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
42+
return ans
5443
}
44+
ans = reversePrint(head.Next)
45+
ans = append(ans, head.Val)
5546
return ans
5647
}
57-
5848
```
5949
{% endtab %}
6050
{% endtabs %}
@@ -79,12 +69,21 @@ func reversePrint(head *ListNode) []int {
7969
{% tab title="Go" %}
8070
```go
8171
func reversePrint(head *ListNode) []int {
82-
ans := make([]int, 0)
8372
if head == nil {
84-
return ans
73+
return []int{}
74+
}
75+
pre, cur, next, ans := &ListNode{}, head, head.Next, []int{}
76+
for cur != nil {
77+
next = cur.Next
78+
cur.Next = pre
79+
80+
pre = cur
81+
cur = next
82+
}
83+
for pre.Next != nil {
84+
ans = append(ans, pre.Val)
85+
pre = pre.Next
8586
}
86-
ans = reversePrint(head.Next)
87-
ans = append(ans, head.Val)
8887
return ans
8988
}
9089
```

0 commit comments

Comments
 (0)