diff --git "a/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" index bfa8e8184131e..ca4eae5ad4265 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" @@ -402,6 +402,33 @@ var reversePrint = function (head) { }; ``` +#### Swift + +```swift +/* public class ListNode { +* public var val: Int +* public var next: ListNode? +* public init(_ val: Int) { +* self.val = val +* self.next = nil +* } +* } +*/ + +class Solution { + func reversePrint(_ head: ListNode?) -> [Int] { + var stack = [Int]() + var current = head + while let node = current { + stack.append(node.val) + current = node.next + } + + return stack.reversed() + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/Solution.swift" new file mode 100644 index 0000000000000..f076edd505df9 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/Solution.swift" @@ -0,0 +1,22 @@ +/* public class ListNode { +* public var val: Int +* public var next: ListNode? +* public init(_ val: Int) { +* self.val = val +* self.next = nil +* } +* } +*/ + +class Solution { + func reversePrint(_ head: ListNode?) -> [Int] { + var stack = [Int]() + var current = head + while let node = current { + stack.append(node.val) + current = node.next + } + + return stack.reversed() + } +} \ No newline at end of file