Skip to content

Commit 4e46a8b

Browse files
committed
Added public init and array init
I added a public init so this can be used in a Playground. I also added an extension to create a linked list from an array of values.
1 parent bad00fe commit 4e46a8b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ extension LinkedList {
187187
}
188188
}
189189

190+
extension LinkedList {
191+
convenience init(array: Array<T>) {
192+
self.init()
193+
194+
for element in array {
195+
self.append(element)
196+
}
197+
}
198+
}
199+
190200
let list = LinkedList<String>()
191201
list.isEmpty // true
192202
list.first // nil

Linked List/LinkedList.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public class LinkedList<T> {
1212
public typealias Node = LinkedListNode<T>
1313

1414
fileprivate var head: Node?
15-
15+
16+
public init() {}
17+
1618
public var isEmpty: Bool {
1719
return head == nil
1820
}
@@ -184,3 +186,13 @@ extension LinkedList {
184186
return result
185187
}
186188
}
189+
190+
extension LinkedList {
191+
convenience init(array: Array<T>) {
192+
self.init()
193+
194+
for element in array {
195+
self.append(element)
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)