swift-algorithm-club icon indicating copy to clipboard operation
swift-algorithm-club copied to clipboard

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

Open beta-yu opened this issue 3 years ago • 1 comments

https://github.com/raywenderlich/swift-algorithm-club/blob/5d551049a02cccd00b046937750fd6d736065f76/LRU%20Cache/LRUCache.swift#L35

beta-yu avatar Jul 12 '21 10:07 beta-yu

    /// 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
    }

beta-yu avatar Jul 12 '21 10:07 beta-yu