is this correct? ``` override fun iterator(): k...
# announcements
s
is this correct?
Copy code
override fun iterator(): kotlin.collections.Iterator<T> {
        return object : kotlin.collections.Iterator<T> {
            var node = head
            override fun hasNext(): Boolean {
                if (node != null) {
                    if (node?.next != null) return true
                }
                return false
            }
            override fun next(): T {
                if (node != null) {
                    val value = node?.value
                    node = node?.next
                    return value as T
                }
                throw Exception("No Such Element")
            }
        }
    }
🧵 3
a
@Smallville7123 Please, could you use thread. It is help you keep your messages bundled and not polluting general channel. Thank you.
s
code looks fine, I'd probably make the nullability explicit just to be sure not to run in unexpected behaviour -> So T is introduced at classlevel (I presume, because you don't do it on the method level). So I'd set T: Any there, just to disallow null. I don't see the definition of head, so I'd set the type of node to T?, because it can be null. you could simplify hasNext to override fun hasNext() = node!=null
s
node
is
Copy code
inner class Node<T>(value: T){
        var value:T = value
        var next: Node<T>? = null
        var previous:Node<T>? = null
    }
    private var head:Node<T>? = null