707.设计链表

function ListNode(val, next) {
    this.val = (val === undefined ? 0 : val)
    this.next = (next === undefined ? null : next)
}

var MyLinkedList = function () {
    this.head = null
};

/** 
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function (index) {
    let cur = this.head
    let i = 0
    while (cur) {
        if (i === index) {
            return cur.val
        }
        i++
        cur = cur.next
    }
    return -1
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function (val) {
    const node = new ListNode(val)
    node.next = this.head
    this.head = node
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function (val) {
    if (!this.head) {
        this.head = new ListNode(val)
        return 
    }
    let cur = this.head
    while (cur) {
        if (cur.next === null) {
            cur.next = new ListNode(val, null)
            return
        }
        cur = cur.next
    }
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function (index, val) {
    // 在链头添加
    if(index === 0) {
        this.addAtHead(val)
        return
    }
    let len = this.size()
    if(index > len) {
        return
    }
    // 在链尾添加
    if(index === len) {
        this.addAtTail(val)
        return
    }

    let i = 1
    let pre = this.head
    let cur = this.head.next
    while (cur) {
        if (index === i) {
            const newNode = new ListNode(val)
            pre.next = newNode
            newNode.next = cur
            return
        }

        i++
        pre = pre.next
        cur = cur.next
    }
};

/** 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function (index) {
    if (!this.head) {
        return
    }
    // 删除表头
    if(index === 0) {
        this.head = this.head.next
        return
    }

    let pre = this.head
    let cur = this.head.next
    let i = 1
    while (cur) {
        if (i === index) {
            pre.next = cur.next
            return
        }

        i++
        pre = pre.next
        cur = cur.next
    }
};

MyLinkedList.prototype.size = function() {
    let len = 0
    let cur = this.head
    while(cur) {
        cur = cur.next
        len++
    }
    return len
}

MyLinkedList.prototype.print = function () {
    let cur = this.head
    const res = []
    while (cur) {
        res.push(cur.val)
        cur = cur.next
    }
    console.log(res)
}

/**
 * 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)
 */
// const myLinkedList = new MyLinkedList();
// myLinkedList.addAtHead(1);
// myLinkedList.print()
// myLinkedList.addAtTail(3);
// myLinkedList.print()
// myLinkedList.addAtIndex(1, 2);    // 链表变为 1->2->3
// myLinkedList.print()
// myLinkedList.addAtIndex(3, 4);    // 链表变为 1->2->3->4
// myLinkedList.print()
// console.log(myLinkedList.get(1));              // 返回 2
// myLinkedList.deleteAtIndex(1);    // 现在,链表变为 1->3
// myLinkedList.print()
// console.log(myLinkedList.get(1));              // 返回 3

// const myLinkedList = new MyLinkedList();
// myLinkedList.addAtHead(5)
// myLinkedList.addAtIndex(1,2)
// myLinkedList.print()
// console.log(myLinkedList.get(1))
// myLinkedList.addAtHead(6)
// myLinkedList.addAtTail(2)
// myLinkedList.print()
// console.log(myLinkedList.get(3))

const myLinkedList = new MyLinkedList();
myLinkedList.print()
myLinkedList.addAtTail(1)
myLinkedList.addAtTail(3)
myLinkedList.print()
console.log(myLinkedList.get(1))