LeetCode707 设计链表

题目

在这里插入图片描述

解题

解题一:单链表

建议使用哨兵节点,如此一来插入和删除可以减少讨论(链表永远不为空),没有使用哨兵节点的方法的代码也贴在下面。

// javascript
var MyLinkedList = function() {
    this.head = new ListNode(0);      // 哨兵节点
    this.size = 0;
};

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

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function(val) {
    this.addAtIndex(0, val);
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function(val) {
    this.addAtIndex(this.size, val);
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function(index, val) {
    if (index < 0 || index > this.size) return;
    // 因为有哨兵节点,所以 0 <= index <= this.size 都能找到前驱节点 
    const node = new ListNode(val);
    let cur = this.head;
    while (index - 1 >= 0) {
        index--;
        cur = cur.next;
    }
    node.next = cur.next;
    cur.next = node;
    this.size++;
};

/** 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function(index) {
    if (index < 0 || index >= this.size) return;
    // 因为有哨兵节点,所以 0 <= index < this.size 都能找到前驱节点 
    let cur = this.head;
    while (index - 1 >= 0) {
        index--;
        cur = cur.next;
    }
    cur.next = cur.next.next;
    this.size--;
};

没有哨兵节点:

// javascript
var MyLinkedList = function() {
    this.head = null;
    this.size = 0;
};

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

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function(val) {
    this.addAtIndex(0, val);
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function(val) {
    this.addAtIndex(this.size, val);
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function(index, val) {
    if (index < 0 || index > this.size) return;
    const node = new ListNode(val);
    if (index === 0) {
        if (this.size === 0) this.head = node;
        else {
            node.next = this.head;
            this.head = node;
        }
    } else {
        let cur = this.head;
        while (index - 1 > 0) {
            index--;
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }
    this.size++;
};

/** 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function(index) {
    if (index < 0 || index >= this.size) return;
    if (index === 0) {
        this.head = this.head.next;
    } else {
        let cur = this.head;
        while (index - 1 > 0) {
            index--;
            cur = cur.next;
        }
        cur.next = cur.next.next;
    }
    this.size--;
};

解题二:双向链表

// javascript
var DoublyListNode = function(val, prev, next) {
    this.val = (val === undefined) ? 0 : val;
    this.prev = (prev === undefined) ? null : prev;
    this.next = (next === undefined) ? null : next;
};

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

MyLinkedList.prototype.getNode = function(index) {
    let cur = null;
    if (index < (this.size >> 1)) {
        cur = this.head;
        for (let i = 0; i < index; ++i) cur = cur.next;
    } else {
        cur = this.tail;
        for (let i = this.size - 1; i > index; --i) {
            cur = cur.prev;
        }
    }
    return cur;
}
/** 
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function(index) {
    if (index < 0 || index >= this.size) return -1;
    const node = this.getNode(index);
    return node.val;
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function(val) {
    const prevHead = this.head;
    this.head = new DoublyListNode(val);
    this.head.next = prevHead;
    if (prevHead !== null) {
        prevHead.prev = this.head;
    } else {
        this.tail = this.head;      // 旧的head是null代表原先没有任何元素
    }
    this.size++;
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function(val) {
    const prevTail = this.tail;
    this.tail = new DoublyListNode(val);
    this.tail.prev = prevTail;
    if (prevTail !== null) {
        prevTail.next = this.tail;
    } else {
        this.head = this.tail;      // 旧的tail是null代表原先没有任何元素
    }
    this.size++;
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function(index, val) {
    if (index < 0 || index > this.size) return;
    if (index === 0) return this.addAtHead(val);
    if (index === this.size) return this.addAtTail(val);
    const curNode = new DoublyListNode(val);
    const nextNode = this.getNode(index);
    const prevNode = nextNode.prev;
    curNode.next = nextNode;
    curNode.prev = prevNode;
    prevNode.next = curNode;
    nextNode.prev = curNode;
    this.size++;
};

/** 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function(index) {
    if (index < 0 || index >= this.size) return;
    // this.size = 1时,如果 index = 0, this.size - 1 = 0
    // 所以 this.size === 1 的处理放在 if (index === 0) / if (index === this.size - 1) 都行
    if (index === 0) {
        if (this.size === 1) {
            this.head = this.tail = null;
        } else {
            this.head = this.head.next;
            this.head.prev = null;
        }
    } else if (index === this.size - 1) {
        this.tail = this.tail.prev;
        this.tail.next = null;
    } else {
        const curNode = this.getNode(index);
        const prevNode = curNode.prev, nextNode = curNode.next;
        prevNode.next = nextNode;
        nextNode.prev = prevNode;
        // curNode.prev.next = curNode.next;
        // curNode.next.prev = curNode.prev;
        // curNode.prev = curNode.next = null; // 可加可不加
    }
    this.size--;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值