mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-24 10:14:44 +08:00
build
This commit is contained in:
@ -643,9 +643,11 @@ comments: true
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack {
|
||||
private var _peek: ListNode? // 将头节点作为栈顶
|
||||
private var _size = 0 // 栈的长度
|
||||
private var _size: Int // 栈的长度
|
||||
|
||||
init() {}
|
||||
init() {
|
||||
_size = 0
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
func size() -> Int {
|
||||
@ -685,8 +687,8 @@ comments: true
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
func toArray() -> [Int] {
|
||||
var node = _peek
|
||||
var res = Array(repeating: 0, count: _size)
|
||||
for i in sequence(first: res.count - 1, next: { $0 >= 0 + 1 ? $0 - 1 : nil }) {
|
||||
var res = Array(repeating: 0, count: size())
|
||||
for i in res.indices.reversed() {
|
||||
res[i] = node!.val
|
||||
node = node?.next
|
||||
}
|
||||
|
Reference in New Issue
Block a user