19
19
20
20
## 解法
21
21
22
- 该题需要将链表转换为数组,且需要反向。由于目标是链表,无法第一时间得知长度,声明等长数组。
22
+ ** 方法一:顺序遍历 + 反转 **
23
23
24
- 解题方案:
24
+ 我们可以顺序遍历链表,将每个节点的值存入数组中,然后将数组反转。
25
25
26
- - 遍历
27
- - 从头到尾遍链表,获取链表长度,声明等长数组;
28
- - 再次遍历并放入数组当中,在数组中的放置顺序是从尾到头。
29
- - 递归
30
- - 记录深度,递归到链表尾部;
31
- - 将深度化为数组长度,将回溯结果正序放入数组当中。
32
- - 动态数组
33
- - 遍历链表,将元素放入数组当中;
34
- - 遍历结束,将数组倒置后返回(` reverse() ` )。
26
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为链表的长度。
27
+
28
+ ** 方法二:递归**
29
+
30
+ 我们可以使用递归的方式,先递归得到 ` head ` 之后的节点反过来的值列表,然后将 ` head ` 的值加到列表的末尾。
31
+
32
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为链表的长度。
35
33
36
34
<!-- tabs:start -->
37
35
@@ -54,9 +52,23 @@ class Solution:
54
52
return ans[::- 1 ]
55
53
```
56
54
57
- ### ** Java**
55
+ ``` python
56
+ # Definition for singly-linked list.
57
+ # class ListNode:
58
+ # def __init__(self, x):
59
+ # self.val = x
60
+ # self.next = None
61
+
62
+ class Solution :
63
+ def reversePrint (self , head : ListNode) -> List[int ]:
64
+ if head is None :
65
+ return []
66
+ ans = self .reversePrint(head.next)
67
+ ans.append(head.val)
68
+ return ans
69
+ ```
58
70
59
- 栈实现:
71
+ ### ** Java **
60
72
61
73
``` java
62
74
/**
@@ -83,8 +95,6 @@ class Solution {
83
95
}
84
96
```
85
97
86
- 先计算链表长度 n,然后创建一个长度为 n 的结果数组。最后遍历链表,依次将节点值存放在数组上(从后往前):
87
-
88
98
``` java
89
99
/**
90
100
* Definition for singly-linked list.
@@ -96,41 +106,62 @@ class Solution {
96
106
*/
97
107
class Solution {
98
108
public int [] reversePrint (ListNode head ) {
99
- if (head == null ) {
100
- return new int [] {};
101
- }
102
109
int n = 0 ;
103
- for (ListNode cur = head; cur != null ; cur = cur. next, ++ n)
104
- ;
110
+ ListNode cur = head;
111
+ for (; cur != null ; cur = cur. next) {
112
+ ++ n;
113
+ }
105
114
int [] ans = new int [n];
106
- for (ListNode cur = head; cur != null ; cur = cur. next) {
115
+ cur = head;
116
+ for (; cur != null ; cur = cur. next) {
107
117
ans[-- n] = cur. val;
108
118
}
109
119
return ans;
110
120
}
111
121
}
112
122
```
113
123
114
- ### ** JavaScript **
124
+ ### ** C++ **
115
125
116
- ``` js
126
+ ``` cpp
117
127
/* *
118
128
* Definition for singly-linked list.
119
- * function ListNode(val) {
120
- * this.val = val;
121
- * this.next = null;
122
- * }
129
+ * struct ListNode {
130
+ * int val;
131
+ * ListNode *next;
132
+ * ListNode(int x) : val(x), next(NULL) {}
133
+ * };
123
134
*/
135
+ class Solution {
136
+ public:
137
+ vector<int > reversePrint(ListNode* head) {
138
+ if (!head) return {};
139
+ vector<int > ans = reversePrint(head->next);
140
+ ans.push_back(head->val);
141
+ return ans;
142
+ }
143
+ };
144
+ ```
145
+
146
+ ```cpp
124
147
/**
125
- * @param {ListNode} head
126
- * @return {number[]}
148
+ * Definition for singly-linked list.
149
+ * struct ListNode {
150
+ * int val;
151
+ * ListNode *next;
152
+ * ListNode(int x) : val(x), next(NULL) {}
153
+ * };
127
154
*/
128
- var reversePrint = function (head ) {
129
- let ans = [];
130
- for (; !! head; head = head .next ) {
131
- ans .unshift (head .val );
155
+ class Solution {
156
+ public:
157
+ vector<int> reversePrint(ListNode* head) {
158
+ vector<int> ans;
159
+ for (; head; head = head->next) {
160
+ ans.push_back(head->val);
161
+ }
162
+ reverse(ans.begin(), ans.end());
163
+ return ans;
132
164
}
133
- return ans;
134
165
};
135
166
```
136
167
@@ -144,59 +175,77 @@ var reversePrint = function (head) {
144
175
* Next *ListNode
145
176
* }
146
177
*/
147
- func reversePrint (head *ListNode ) []int {
148
- ans := []int {}
149
- for head != nil {
150
- ans = append ([]int {head.Val }, ans...)
151
- head = head.Next
178
+ func reversePrint (head *ListNode ) (ans []int ) {
179
+ for ; head != nil ; head = head.Next {
180
+ ans = append (ans, head.Val )
152
181
}
153
- return ans
182
+ for i , j := 0 , len (ans)-1 ; i < j; i, j = i+1 , j-1 {
183
+ ans[i], ans[j] = ans[j], ans[i]
184
+ }
185
+ return
154
186
}
155
187
```
156
188
157
- ### ** C++**
189
+ ``` go
190
+ /* *
191
+ * Definition for singly-linked list.
192
+ * type ListNode struct {
193
+ * Val int
194
+ * Next *ListNode
195
+ * }
196
+ */
197
+ func reversePrint (head *ListNode ) (ans []int ) {
198
+ if head == nil {
199
+ return
200
+ }
201
+ ans = reversePrint (head.Next )
202
+ ans = append (ans, head.Val )
203
+ return
204
+ }
205
+ ```
158
206
159
- 递归实现:
207
+ ### ** JavaScript **
160
208
161
- ``` cpp
209
+ ``` js
162
210
/**
163
211
* Definition for singly-linked list.
164
- * struct ListNode {
165
- * int val;
166
- * ListNode *next;
167
- * ListNode(int x) : val(x), next(NULL) {}
168
- * };
212
+ * function ListNode(val) {
213
+ * this.val = val;
214
+ * this.next = null;
215
+ * }
169
216
*/
170
- class Solution {
171
- public:
172
- vector<int > reversePrint(ListNode* head) {
173
- if (!head) return {};
174
- vector<int > ans = reversePrint(head->next);
175
- ans.push_back(head->val);
176
- return ans;
217
+ /**
218
+ * @param {ListNode} head
219
+ * @return {number[]}
220
+ */
221
+ var reversePrint = function (head ) {
222
+ let ans = [];
223
+ for (; !! head; head = head .next ) {
224
+ ans .unshift (head .val );
177
225
}
226
+ return ans;
178
227
};
179
228
```
180
229
181
- 栈实现:
182
-
183
- ```cpp
184
- class Solution {
185
- public:
186
- vector<int> reversePrint(ListNode* head) {
187
- stack<int> stk;
188
- vector<int> ans;
189
- ListNode *p = head;
190
- while (p) {
191
- stk.push(p->val);
192
- p = p->next;
193
- }
194
- while (!stk.empty()) {
195
- ans.push_back(stk.top());
196
- stk.pop();
197
- }
198
- return ans;
230
+ ``` js
231
+ /**
232
+ * Definition for singly-linked list.
233
+ * function ListNode(val) {
234
+ * this.val = val;
235
+ * this.next = null;
236
+ * }
237
+ */
238
+ /**
239
+ * @param {ListNode} head
240
+ * @return {number[]}
241
+ */
242
+ var reversePrint = function (head ) {
243
+ if (! head) {
244
+ return [];
199
245
}
246
+ const ans = reversePrint (head .next );
247
+ ans .push (head .val );
248
+ return ans;
200
249
};
201
250
```
202
251
@@ -226,8 +275,6 @@ function reversePrint(head: ListNode | null): number[] {
226
275
227
276
### ** Rust**
228
277
229
- 动态数组:
230
-
231
278
``` rust
232
279
// Definition for singly-linked list.
233
280
// #[derive(PartialEq, Eq, Clone, Debug)]
@@ -259,8 +306,6 @@ impl Solution {
259
306
}
260
307
```
261
308
262
- 遍历:
263
-
264
309
``` rust
265
310
// Definition for singly-linked list.
266
311
// #[derive(PartialEq, Eq, Clone, Debug)]
0 commit comments