Skip to content

Instantly share code, notes, and snippets.

@robtimp
Last active April 10, 2018 20:39
Show Gist options
  • Save robtimp/0d1cfd8f58e2efc463dfed585e40e48e to your computer and use it in GitHub Desktop.
Save robtimp/0d1cfd8f58e2efc463dfed585e40e48e to your computer and use it in GitHub Desktop.
ListNode with description
public class ListNode: ExpressibleByArrayLiteral {
public var value: Int
public var next: ListNode?
public init(_ value: Int, next: ListNode? = nil) {
self.value = value
self.next = next
}
public required init(arrayLiteral elements: Int...) {
self.value = elements[0]
var current = self
for element in elements.dropFirst() {
let next = ListNode(element)
current.next = next
current = next
}
}
}
extension ListNode: CustomStringConvertible {
public var description: String {
let description = "\(value) -> "
if let next = next {
return description + next.description
} else {
return description + "nil"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment