Leetcode707 设计链表 双向链表实现

  JAVA 双向链表实现:

class MyLinkedList {
        private Node begin;
        private Node end;
        private int len;

        private class Node {
            int val;
            Node next;
            Node pre;

            Node(int val) {
                this.val = val;
            }
        }

        /**
         * Initialize your data structure here.
         */
        public MyLinkedList() {
            this.begin = new Node(0);
            this.end = new Node(0);
            begin.next = end;
            end.pre = begin;
            this.len = 0;
        }

        /**
         * Get the value of the index-th node in the linked list. If the index is invalid, return -1.
         */
        public int get(int index) {
            Node node = this.getNode(index);
            return node == null ? -1 : node.val;
        }

        public Node getNode(int index) {
            if (index < 0 || index >= len) return null;
            int mid = len / 2;
            Node node;
            if (index >= mid) {
                node = end;
                for (int i = 0; i < len - index; i++) node = node.pre;
            } else {
                node = this.begin;
                for (int i = 0; i <= index; i++) node = node.next;
            }
            return node;
        }

        /**
         * 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.
         */
        public void addAtHead(int val) {
            Node node = new Node(val);
            Node next = this.begin.next;
            this.begin.next = node;
            next.pre = node;
            node.next = next;
            node.pre = this.begin;
            this.len++;
        }

        /**
         * Append a node of value val to the last element of the linked list.
         */
        public void addAtTail(int val) {
            Node node = new Node(val);
            Node pre = end.pre;
            pre.next = node;
            this.end.pre = node;
            node.next = this.end;
            node.pre = pre;
            this.len++;
        }

        /**
         * 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.
         */
        public void addAtIndex(int index, int val) {
            if (index < 0) return;
            Node before = this.getNode(index);
            if (before == null) before = this.end;
            Node pre = before.pre;
            Node curr = new Node(val);
            pre.next = curr;
            before.pre = curr;
            curr.next = before;
            curr.pre = pre;
            this.len++;
        }

        /**
         * Delete the index-th node in the linked list, if the index is valid.
         */
        public void deleteAtIndex(int index) {
            if (index < 0 || index >= len) return;
            Node node = this.getNode(index);
            node.pre.next = node.next;
            node.next.pre = node.pre;
            node = null;
            this.len--;
        }
    }

  JS 双向链表实现:

/**
 * Initialize your data structure here.
 */
var MyLinkedList = function () {
    this.begin = new Node();
    this.end = new Node();
    this.len = 0;
    this.begin.next = this.end;
    this.end.pre = this.begin;
};

var Node = function (val, pre, next) {
    this.val = val;
    this.pre = pre;
    this.next = next;
}

/**
 * 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) {
    let node = this.getNode(index);
    return !node ? -1 : node.val;
};

MyLinkedList.prototype.getNode = function (index) {
    if (index < 0 || index >= this.len) return null;
    let node, mid = this.len / 2;
    if (index < mid) {
        node = this.begin;
        for (let i = 0; i <= index; i++) node = node.next;
    } else {
        node = this.end;
        for (let i = 0; i < this.len - index; i++) node = node.pre;
    }
    return node;
}

/**
 * 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) {
    let next = this.begin.next;
    let node = new Node(val, this.begin, next);
    this.begin.next = node;
    next.pre = node;
    this.len++;
};

/**
 * Append a node of value val to the last element of the linked list.
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function (val) {
    let pre = this.end.pre;
    let node = new Node(val, pre, this.end);
    pre.next = node;
    this.end.pre = node;
    this.len++;
};

/**
 * 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) return;
    let before = this.getNode(index);
    if (!before) before = this.end;
    let node = new Node(val, before.pre, before);
    before.pre.next = node;
    before.pre = node;
    this.len++;
};

/**
 * Delete the index-th node in the linked list, if the index is valid.
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function (index) {
    let node = this.getNode(index);
    if (!node) return;
    node.pre.next = node.next;
    node.next.pre = node.pre;
    this.len--;
};

/**
 * 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)
 */

当你看清人们的真相,于是你知道了,你可以忍受孤独
原文地址:https://www.cnblogs.com/niuyourou/p/14428093.html