kodecocodes/swift-algorithm-club

LinkedList Get Last Node Cost NOT O(1) Time.

beta-yu opened this issue · 1 comments

    /// Computed property to iterate through the linked list and return the last node in the list (if any)
    public var last: Node? {
        guard var node = head else {
            return nil
        }
        
        while let next = node.next {
            node = next
        }
        return node
    }