40-leetCode: 707. 设计链表

326 阅读2分钟

题目

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。 addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。 addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。 addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。 deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 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

解题思路

本题的思路是利用链表的特性进行封装链表的各种方法,定义类的时候,初始化好链表的头部,尾部,长度,还有创建节点的方法。新增节点的时候,注意新增头部,考虑是否同时新增尾部,新增头部和中间亦然。查找来链表,需要判断是否在长度内。删除节点依然。删除节点还需看是否删除的头尾,同样要更新头尾。当然,每次对链表的增删都应该更新链表的长度。

// 构造函数,初始化头部,尾部, 长度为0 
var MyLinkedList = function() {
    this.head = null;
    this.tail = null;
    this.length = 0;
};
/** 
 * @param {number} val
 * @return {number}
 */
// 创建节点类
var ListNode = function(val) {
    this.val = val;
    this.next = null;
}
/** 
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function(index) {
    // 如果传入的index不在链表长度范围内,直接返回 -1;
    if(index < 0 || index >= this.length) {
        return -1;
    }
    let i = 0, cur = this.head;
    // 通过遍历,得到index位置的节点
    while (i < index) {
        cur = cur.next;
        i++;
    }
    return cur.val;
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function(val) {
    const lastHead = this.head; // 插入头节点需要先暂存上一个头节点
    const node = new ListNode(val);
    this.head = node;
    this.head.next = lastHead; // 让新的头节点指向暂存的头节点,形成新头节点的链表
    // 判断是否有尾节点,没有的话,插入头节点也就是插入尾节点了
    if (!this.tail) {
        this.tail = node;
        this.tail.next = null;
    }
    this.length++;
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function(val) {
    // 插入尾节点跟插入头节点一样的判断
    const lastTail = this.tail;
    const node = new ListNode(val);
    this.tail = node;
    if (lastTail) {      
        lastTail.next = this.tail;
    }
    if (!this.head) {
        this.head = node;
        this.head.next = null;
    }
    this.length ++;
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function(index, val) {
    // 先分别判断是否是插入头尾节点,最后才是插入其他位置节点
    if (index === this.length) {
        this.addAtTail(val)
    } else if (index <= 0) {
        this.addAtHead(val)
    } else if (index > 0 && index < this.length) {
        let i = 0
        let prev = this.head
        // 找到pre节点,然后插入到pre后面即可
        while (i < index - 1) {
            prev = prev.next
            i ++
        }
        const node = new ListNode(val)
        node.next = prev.next
        prev.next = node
        this.length ++
    }
};

/** 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function(index) {
    if (index > 0 && index < this.length) {
        let i = 0
        let prev = null
        let cur = this.head
        // 关键是找到pre节点和curr节点
        while (i < index) {
            prev = cur
            cur = cur.next
            i ++
        }
        // 删除节点即跳过index节点
        prev.next = cur.next
        if (index === this.length - 1) {
            this.tail = prev
        }
        this.length --
    } else if (index === 0) {
        this.head = this.head.next
        this.length --
    }
};