Code "/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

class LRUCache {
    private readonly nodeMap: Map<number, ListNode>;
    private last: ListNode | null;
    private size: number;
    private readonly capacity: number;

    constructor(capacity: number) {
        this.size = 0;
        this.capacity = capacity;
        this.last = null;
        this.nodeMap = new Map();
    }

    get(key: number): number {
        return this.nodeMap.has(key) ? this.nodeMap.get(key)!.val : -1;
    }

    put(key: number, value: number): void {
        if (this.size === this.capacity) {
            if (!this.nodeMap.has(key)) {
                const newNode = new ListNode(value);
                newNode.next = this.last!.next!.next;
                this.last!.next = newNode;
                this.last = newNode;
            } else {
                const node = this.nodeMap.get(key)!;
                let current = this.last;
                while (current && current.next !== node) {
                    current = current.next;
                }
                current!.next = current!.next!.next;
                const newNode = new ListNode(value);
                newNode.next = this.last!.next!.next;
                this.last!.next = newNode;
                this.last = newNode;
            }
        } else if (this.size < this.capacity) {
            if (!this.nodeMap.has(key)) {
                const newNode = new ListNode(value);
                newNode.next = this.last!.next!.next;
                this.last!.next = newNode;
                this.last = newNode;
            } else {
                const node = this.nodeMap.get(key)!;
                let current = this.last;
                while (current && current.next !== node) {
                    current = current.next;
                }
                current!.next = current!.next!.next;
                const newNode = new ListNode(value);
                newNode.next = this.last!.next!.next;
                this.last!.next = newNode;
                this.last = newNode;
            }
            this.size++;
        }
        this.nodeMap.set(key, this.last!);
    }
}
" error "                newNode.next = this.last!.next!.next;
                                          ^
TypeError: Cannot read properties of null (reading 'next')
    Line 40: Char 43 in solution.ts (LRUCache.put)
    Line 83: Char 26 in solution.ts (helper_select_method)
    Line 131: Char 22 in solution.ts (Object.<anonymous>)
    Line 16: Char 8 in runner.js (Object.runner)
    Line 61: Char 26 in solution.ts (Object.<anonymous>)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)"