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)
 */
复制代码

posted @   牛有肉  阅读(92)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示