@@ -100,9 +100,11 @@ class Solution {
100
100
class Solution {
101
101
public:
102
102
vector<int > reversePrint(ListNode* head) {
103
- if (!head) return {};
104
- vector<int > ans = reversePrint(head->next);
105
- ans.push_back(head->val);
103
+ vector<int > ans;
104
+ for (; head; head = head->next) {
105
+ ans.push_back(head->val);
106
+ }
107
+ reverse(ans.begin(), ans.end());
106
108
return ans;
107
109
}
108
110
};
@@ -145,11 +147,11 @@ func reversePrint(head *ListNode) (ans []int) {
145
147
*/
146
148
147
149
function reversePrint(head : ListNode | null ): number [] {
148
- let ans: number [] = [];
149
- for (; !! head ; head = head .next ) {
150
- ans .unshift (head .val );
150
+ const ans: number [] = [];
151
+ for (; head ; head = head .next ) {
152
+ ans .push (head .val );
151
153
}
152
- return ans ;
154
+ return ans . reverse () ;
153
155
}
154
156
```
155
157
@@ -174,14 +176,14 @@ function reversePrint(head: ListNode | null): number[] {
174
176
// }
175
177
impl Solution {
176
178
pub fn reverse_print (head : Option <Box <ListNode >>) -> Vec <i32 > {
177
- let mut arr : Vec <i32 > = vec! [];
179
+ let mut ans : Vec <i32 > = vec! [];
178
180
let mut cur = head ;
179
181
while let Some (node ) = cur {
180
- arr . push (node . val);
182
+ ans . push (node . val);
181
183
cur = node . next;
182
184
}
183
- arr . reverse ();
184
- arr
185
+ ans . reverse ();
186
+ ans
185
187
}
186
188
}
187
189
```
@@ -191,21 +193,21 @@ impl Solution {
191
193
``` js
192
194
/**
193
195
* Definition for singly-linked list.
194
- * function ListNode(val) {
195
- * this.val = val;
196
- * this.next = null;
196
+ * function ListNode(val, next ) {
197
+ * this.val = ( val===undefined ? 0 : val)
198
+ * this.next = (next===undefined ? null : next)
197
199
* }
198
200
*/
199
201
/**
200
202
* @param {ListNode} head
201
203
* @return {number[]}
202
204
*/
203
205
var reversePrint = function (head ) {
204
- let ans = [];
205
- for (; !! head; head = head .next ) {
206
- ans .unshift (head .val );
206
+ const ans = [];
207
+ for (; head; head = head .next ) {
208
+ ans .push (head .val );
207
209
}
208
- return ans;
210
+ return ans . reverse () ;
209
211
};
210
212
```
211
213
@@ -217,20 +219,49 @@ var reversePrint = function (head) {
217
219
* public class ListNode {
218
220
* public int val;
219
221
* public ListNode next;
220
- * public ListNode(int x) { val = x; }
222
+ * public ListNode(int val=0, ListNode next=null) {
223
+ * this.val = val;
224
+ * this.next = next;
225
+ * }
221
226
* }
222
227
*/
223
- public class Solution {
224
- public int [] ReversePrint (ListNode head ) {
225
- List < int > ans = new List <int >();
226
- while (head != null ) {
227
- ans .Add (head .val );
228
- head = head .next ;
229
- }
230
- ans .Reverse ();
231
- return ans .ToArray ();
232
- }
233
- }
228
+ public class Solution {
229
+ public int [] ReversePrint (ListNode head ) {
230
+ List < int > ans = new List <int >();
231
+ for (; head != null ; head = head .next ) {
232
+ ans .Add (head .val );
233
+ }
234
+ ans .Reverse ();
235
+ return ans .ToArray ();
236
+ }
237
+ }
238
+ ```
239
+
240
+ #### Swift
241
+
242
+ ``` swift
243
+ /* public class ListNode {
244
+ * public var val: Int
245
+ * public var next: ListNode?
246
+ * public init(_ val: Int) {
247
+ * self.val = val
248
+ * self.next = nil
249
+ * }
250
+ * }
251
+ */
252
+
253
+ class Solution {
254
+ func reversePrint (_ head : ListNode? ) -> [Int ] {
255
+ var stack = [Int ]()
256
+ var current = head
257
+ while let node = current {
258
+ stack.append (node.val )
259
+ current = node.next
260
+ }
261
+
262
+ return stack.reversed ()
263
+ }
264
+ }
234
265
```
235
266
236
267
<!-- tabs: end -->
@@ -308,11 +339,11 @@ class Solution {
308
339
class Solution {
309
340
public:
310
341
vector<int > reversePrint(ListNode* head) {
311
- vector<int > ans;
312
- for (; head; head = head->next) {
313
- ans.push_back(head->val);
342
+ if (!head) {
343
+ return {};
314
344
}
315
- reverse(ans.begin(), ans.end());
345
+ vector<int > ans = reversePrint(head->next);
346
+ ans.push_back(head->val);
316
347
return ans;
317
348
}
318
349
};
@@ -402,33 +433,6 @@ var reversePrint = function (head) {
402
433
};
403
434
```
404
435
405
- #### Swift
406
-
407
- ``` swift
408
- /* public class ListNode {
409
- * public var val: Int
410
- * public var next: ListNode?
411
- * public init(_ val: Int) {
412
- * self.val = val
413
- * self.next = nil
414
- * }
415
- * }
416
- */
417
-
418
- class Solution {
419
- func reversePrint (_ head : ListNode? ) -> [Int ] {
420
- var stack = [Int ]()
421
- var current = head
422
- while let node = current {
423
- stack.append (node.val )
424
- current = node.next
425
- }
426
-
427
- return stack.reversed ()
428
- }
429
- }
430
- ```
431
-
432
436
<!-- tabs: end -->
433
437
434
438
<!-- solution: end -->
0 commit comments