Ray-56 / like-algorithms

每天一道算法题,突破自己
2 stars 1 forks source link

✅707. 设计链表 #148

Open Ray-56 opened 4 years ago

Ray-56 commented 4 years ago

707. 设计链表

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-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
Ray-56 commented 4 years ago
function Node(val) {
    this.val = val;
    this.next = null;
}

/**
 * Initialize your data structure here.
 */
var MyLinkedList = function() {
    this.head = 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) return -1;

    let cur = this.head;
    for (let i = 0; i < index; i++) {
        if (!cur.next) return -1;
        cur = cur.next;
    }

    return cur ? cur.val : -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) {
    const newHead = new Node(val);
    newHead.next = this.head;
    this.head = newHead;
};

/**
 * Append a node of value val to the last element of the linked list. 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function(val) {
    if (!this.head) {
        this.head = new Node(val);
    }
    let cur = this.head;

    while (cur.next) {
        cur = cur.next;
    }

    cur.next = new Node(val);
};

/**
 * 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 <= 0) {
        this.addAtHead(val);
        return;
    }
    let cur = this.head;
    for (let i = 0; i < index - 1; i++) {
        if (!cur || !cur.next) return;
        cur = cur.next;
    }
    if (!cur) return;
    const next = cur.next;
    cur.next = new Node(val);
    cur.next.next = next;
};

/**
 * 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) return;
    if (index === 0) {
        this.head = this.head.next;
        return;
    }

    let cur = this.head;
    index--;
    while (index > 0 && cur.next) {
        cur = cur.next;
        index--;
    }
    if (!cur.next) return null
    if (!cur.next.next) return cur.next = null
    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)
 */