Skip to content

Commit 4db3cd4

Browse files
authored
feat: add swift implementation to lcof problem: No.06 (#2853)
1 parent 5301c76 commit 4db3cd4

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lcof/面试题06. 从尾到头打印链表/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,33 @@ var reversePrint = function (head) {
402402
};
403403
```
404404

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+
405432
<!-- tabs:end -->
406433

407434
<!-- solution:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* public class ListNode {
2+
* public var val: Int
3+
* public var next: ListNode?
4+
* public init(_ val: Int) {
5+
* self.val = val
6+
* self.next = nil
7+
* }
8+
* }
9+
*/
10+
11+
class Solution {
12+
func reversePrint(_ head: ListNode?) -> [Int] {
13+
var stack = [Int]()
14+
var current = head
15+
while let node = current {
16+
stack.append(node.val)
17+
current = node.next
18+
}
19+
20+
return stack.reversed()
21+
}
22+
}

0 commit comments

Comments
 (0)