Last active
April 10, 2018 20:39
-
-
Save robtimp/0d1cfd8f58e2efc463dfed585e40e48e to your computer and use it in GitHub Desktop.
ListNode with description
This file contains hidden or 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
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