/** * @param {number} index * @return {number} */ MyLinkedList.prototype.get = function (index) { let cur = this for (let i = 0; i <= index; i++) { if (cur.next) { cur = cur.next } else { return -1 } } return cur.val }
/** * @param {number} val * @return {void} */ MyLinkedList.prototype.addAtHead = function (val) { const newNode = newMyLinkedList() newNode.val = val newNode.next = this.next // 从这一行可以清楚的看出,这是添加虚拟节点的操作方式 this.next = newNode }
/** * @param {number} val * @return {void} */ MyLinkedList.prototype.addAtTail = function (val) { const newNode = newMyLinkedList() newNode.val = val newNode.next = null let cur = this while (cur.next) { cur = cur.next } cur.next = newNode }
/** * @param {number} index * @param {number} val * @return {void} */ MyLinkedList.prototype.addAtIndex = function (index, val) { const newNode = newMyLinkedList() newNode.val = val let cur = this for (let i = 0; i < index; i++) { if (cur.next) { cur = cur.next } else { return } } newNode.next = cur.next cur.next = newNode }
/** * @param {number} index * @return {void} */ MyLinkedList.prototype.deleteAtIndex = function (index) { let cur = this for (let i = 0; i < index; i++) { if (cur.next) { cur = cur.next } else { return } } if (!cur.next) return cur.next = cur.next.next }
/** * 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) */
MyLinkedList.prototype.get = function (index) { let cur = this for (let i = 0; i <= index; i++) { if (cur.next) { cur = cur.next } else { return -1 } } return cur.val }
/** * @param {number} val * @return {void} */ MyLinkedList.prototype.addAtHead = function (val) { const newNode = newMyLinkedList() newNode.val = val newNode.next = this.next newNode.prev = this this.next = newNode }
/** * @param {number} val * @return {void} */ MyLinkedList.prototype.addAtTail = function (val) { const newNode = newMyLinkedList() newNode.val = val newNode.next = null let cur = this while (cur.next) { cur = cur.next } newNode.prev = cur cur.next = newNode }
/** * @param {number} index * @param {number} val * @return {void} */ MyLinkedList.prototype.addAtIndex = function (index, val) { const newNode = newMyLinkedList() newNode.val = val let cur = this for (let i = 0; i < index; i++) { if (cur.next) { cur = cur.next } else { return } } newNode.next = cur.next newNode.prev = cur cur.next = newNode }
/** * @param {number} index * @return {void} */ MyLinkedList.prototype.deleteAtIndex = function (index) { let cur = this for (let i = 0; i < index; i++) { if (cur.next) { cur = cur.next } else { return } } if (!cur.next) return cur.next.prev = cur cur.next = cur.next.next }
/** * 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) */