Last active
July 29, 2018 12:21
-
-
Save eneko/3ec52e4f2d05434e28b2 to your computer and use it in GitHub Desktop.
Linked List in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// A list is either empty or it is composed of a first element (head) | |
/// and a tail, which is a list itself. | |
/// | |
/// See http://www.enekoalonso.com/projects/99-swift-problems/#linked-lists | |
class List<T> { | |
var value: T | |
var nextItem: List<T>? | |
convenience init?(_ values: T...) { | |
self.init(Array(values)) | |
} | |
init?(_ values: [T]) { | |
guard let first = values.first else { | |
return nil | |
} | |
value = first | |
nextItem = List(Array(values.suffix(from: 1))) | |
} | |
} |
Hey Scott, thank you for your feedback. It is updated to Swift 4 now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(Swift 4 warning for line 9) Parameters may not have the 'var' specifier. I like your 99 problems website btw.