设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点> 将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
 

示例:

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2); //链表变为1-> 2-> 3
linkedList.get(1); //返回2
linkedList.deleteAtIndex(1); //现在链表是1-> 3
linkedList.get(1); //返回3

 
提示:

所有val值都在 [1, 1000] 之内。
操作次数将在  [1, 1000] 之内。
请不要使用内置的 LinkedList 库。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-linked-list

暂时先不写注释了,因为下标问题折腾了好几次。本地都行,leetcode一跑就不对。之后再把注释补上。


注释补上了,有问题再修改

/**
 * Initialize your data structure here.
 */
var MyLinkedList = function () {
    this.head = null
    this.tail = null
    this.length = 0
};

var listNode = function (val) {
    this.val = val
    this.next = null
}

/**
 * Get the value of the index-th node in the linked list. If the index is invalid, return -1. 
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function (index) {
    //判断传入的下标是否在范围内
    if (index >= 0 && index < this.length) {
        let i = 0
    //先将node变量设置为head节点
        let node = this.head
    //循环赋值node节点为node.next
        while (i < index) {
            node = node.next
            i++
        }
    //返回得到的node.val
        return node.val
    } else {
    //如果不在范围内直接返回-1
        return -1
    }
}

/**
 * Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function (val) {
    //保存为当前head节点
    const lastHead = this.head
    //new 一个新的节点对象
    const newNode = new listNode(val)
    //链表的head节点为新的节点
    this.head = newNode
    //链表的head节点的next指向lastHead
    this.head.next = lastHead
    //如果链表的tail的节点不存在,则tail节点为新节点
    if (!this.tail) {
        this.tail = newNode
    }
    //链表长度加1
    this.length++
}

/**
 * Append a node of value val to the last element of the linked list. 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function (val) {
    //未添加新节点前的tail节点
    const lastTail = this.tail
    const newNode = new listNode(val)
    this.tail = newNode
    //如果之前tail节点存在则将之前的tail节点next指向现在的tail节点
    if (lastTail) {
        lastTail.next = this.tail
    }
    //如果链表head节点不存在,则head节点为新的节点
    if (!this.head) {
        this.head = newNode
    }
    this.length++
}

/**
 * Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function (index, val) {
    //判断索引是否等于链表长度,如果相等则在链表尾部插入节点
    if (index === this.length) {
        this.addAtTail(val)
    //判断索引是否小于1,如果符合则在链表头部插入节点
    } else if (index < 1) {
        this.addAtHead(val)
    //如果索引不符合上面两种
    } else if (index > 0 && index < this.length) {
        let i = 0
        let node = this.head
    //因为插入节点位置为索引前,所以循环时index-1
        while (i < index - 1) {
            node = node.next
            i++
        }
        const newNode = new listNode(val)
    //找到节点先将新节点的next指向node的next
        newNode.next = node.next
    //然后node的next指向新节点,即完成插入节点操作
        node.next = newNode
        this.length++
    }
}

/**
 * Delete the index-th node in the linked list, if the index is valid. 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function (index) {
    //判断索引是否超出链表范围
    if (index > 0 && index < this.length) {
        let i = 0
        let node = this.head
    //这里我们需要找到删除节点的前一位所以index-1
        while (i < index - 1) {
            node = node.next
            i++
        }
    //这里找到的是索引为index-1的节点,将此节点的next指向index+1的节点,所以node.next=node.next.next;
        node.next = node.next.next
    //如果删除节点为最后一个节点,则直接将tail节点变更为index-1所在位置的节点
        if (index === this.length - 1) {
            this.tail = node
        }
        this.length--
    //如果删除节点为第一个节点则将head节点设置为head.next所指向的节点
    } else if (index === 0) {
        this.head = this.head.next
        this.length--
    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * var obj = new MyLinkedList()
 * var param_1 = obj.get(index)
 * obj.addAtHead(val)
 * obj.addAtTail(val)
 * obj.addAtIndex(index,val)
 * obj.deleteAtIndex(index)
 */