存档-链表设计JS/TS版

题目:https://leetcode.cn/problems/design-linked-list/description/
设计链表

TS版

class MyListNode {
    public val: number;
    public next: MyListNode | null;
    constructor(val?: number, next?: MyListNode | null) {
        this.val = val === undefined ? 0 : val;
        this.next = next === undefined ? null : next;
    }
}

class MyLinkedList {
    // 记录链表长度
    private size: number;
    private head: MyListNode | null;
    private tail: MyListNode | null;

    constructor() {
        this.size = 0;
        this.head = null;
        this.tail = null;
    }

    getNode(index: number) {
        let count = 0;
        let curNode = new MyListNode(0, this.head);
        while(count <= index) {
            curNode = curNode.next;
            count ++;
        }
        return curNode;
    }

    get(index: number): number {
        // console.log(this.head, this.tail);
        // 索引无效的情况
        if (index < 0 || index >= this.size) {
            return -1;
        }
        let curNode = this.getNode(index);
        return curNode.val;
    }

    addAtHead(val: number): void {
        const newNode = new MyListNode(val, this.head);
        this.head = newNode;
        if(!this.tail) {
            this.tail = newNode;
        }
        this.size++;
    }

    addAtTail(val: number): void {
        const newNode = new MyListNode(val, null);
        if (this.tail) {
            this.tail.next = newNode;
        } else {
            this.head = newNode;
        }
        this.tail = newNode;
        this.size++;
    }

    addAtIndex(index: number, val: number): void {
        if (index === this.size) {
            this.addAtTail(val);
            return;
        }
        if (index > this.size) {
            return;
        }
        if (index <= 0) {
            this.addAtHead(val);
            return;
        }
        const curNode = new MyListNode(val, null);
        const preNode = this.getNode(index - 1);
        curNode.next = preNode.next;
        preNode.next = curNode;
        this.size++;
    }

    deleteAtIndex(index: number): void {
        if (index < 0 || index >= this.size) {
            return;
        }
        if (index === 0) {
            this.head = this.head.next;
            if (index === this.size - 1) {
                this.tail = null;
            }
            this.size--;
            return;
        }
        const preNode = this.getNode(index - 1);
        preNode.next = preNode.next.next;
        if (index === this.size - 1) {
            this.tail = preNode;
        }
        this.size--;
    }
}

JS版

class LinkNode {
    constructor(val, next) {
        this.val = val;
        this.next = next;
    }
}

var MyLinkedList = function() {
    this._size = 0;
    this._tail = null;
    this._head = null;
};

MyLinkedList.prototype.getNode = function(index) {
    // 虚拟头节点
    let cur = new LinkNode(0, this._head);
    while(index-- >= 0) {
        cur = cur.next;
    }
    return cur;
}

/** 
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function(index) {
    if (index < 0 || index >= this._size) return -1;
    return this.getNode(index).val;
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function(val) {
    const node = new LinkNode(val, this._head);
    this._head = node;
    this._size++;
    if (!this._tail) {
        this._tail = node;
    }
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function(val) {
    const node = new LinkNode(val, null);
    this._size++;
    if(this._tail) {
        this._tail.next = node;
        this._tail = node;
        return;
    }
    this._tail = node;
    this._head = node;
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function(index, val) {
    if(index > this._size) return;
    if(index <= 0) {
        this.addAtHead(val);
        return;
    }
    if(index === this._size) {
        this.addAtTail(val);
        return;
    }
    // 获取目标节点的上一个的节点
    const node = this.getNode(index - 1);
    node.next = new LinkNode(val, node.next);
    this._size++;
};

/** 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function(index) {
    if(index < 0 || index >= this._size) return;
    if(index === 0) {
        this._head = this._head.next;
        // 如果删除的这个节点同时是尾节点,要处理尾节点
        if(index === this._size - 1){
            this._tail = this._head
        }
        this._size--;
        return;
    }
    // 获取目标节点的上一个的节点
    const node = this.getNode(index - 1);    
    node.next = node.next.next;
    // 处理尾节点
    if(index === this._size - 1) {
        this._tail = node;
    }
    this._size--;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值