leetcode记录:难度简单:707. Design Linked List(设计链表)

题目出处:https://leetcode.com/problems/design-linked-list/

题目描述:

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

在链表类中实现这些函数:

get(index):获取链表中第一个索引节点的值。如果索引无效,则返回-1。

addAtHead(val):在链表的第一个元素之前添加一个值为val的节点。插入之后,新节点将是链表的第一个节

addAtTail(val):向链表的最后一个元素追加一个值为val的节点。

addAtIndex(index, val):在链表的第一个索引节点之前添加一个值为val的节点。如果index等于链表的长度,节点将被追加到链表的末尾。如果索引大于长度,则不插入节点。

deleteAtIndex(index):如果索引有效,则删除链表中的第一个索引节点。

Example 1:

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1, 2);  // linked list becomes 1->2->3
linkedList.get(1);            // returns 2
linkedList.deleteAtIndex(1);  // now the linked list is 1->3
linkedList.get(1);            // returns 3

思路:

简单来说,链表有2个部分,一个是数据本身,一个是指向下一个数据的指针。所以:

构造一个函数,可以创建新的空链表,在它的原型链上构造它的添加、删除的各个函数实现其功能。

新建的空链表new MyLinkedList会实例化一个对象出来,这个对象{"head":null,"size":0},注意这个head属性实际也是个对象,再调用原型链上的add方法时,就会调用另外一个构造函数,创造class Node的实例,这个实例也是对象,{“var”:val,“next”:null},这个next属性也是对象。。

所以MylinkedList创建的实例,只是记录了属性size的大小,属性head是写死的。

添加的数据(即class Node的实例)是一层一层作为上一层的next属性连接起来的。cur是指针,每一次cur = this.head,都是把这个指针提前到头部,再循环至所要求的位置,再操作。调用class Node只是创建了一个数据出来,因为新建的数据next没有被赋值,所以是没有进入链表的,定义next属性就可以,增删检查

代码:

class Node {
    constructor(val) {
        this.val = val;
        this.next = null;
    }
}

/**
 * Initialize your data structure here.
 */
var MyLinkedList = function(val) {
    this.head = null;
    this.size = 0;
};

/**
 * 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 || index >= this.size ) {
        return -1;
    }

    let cur = this.head;
    for (let i=0; i < index; i++) {
        cur = cur.next;
    }
    return cur.val;
};

/**
 * 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 cur = this.head;
    this.head = new Node(val);
    this.head.next = cur;
    this.size++;
};

/**
 * 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 cur = this.head;
    while (cur.next) {
        cur = cur.next;
    }

    let newNode = new Node(val);

    if (!cur) {
        this.head = newNode
    } else {
        cur.next = newNode
    }
    this.size++;
};

/**
 * 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;
    }

    if (index === 0) {
        this.addAtHead(val);
        return;
    }

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

    if (!cur) {
        return;
    }

    let next = cur.next;
    cur.next = new Node(val);
    cur.next.next = next;
    this.size++;
};

/**
 * 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 || index >= this.size) {
        return;
    }

    this.size--;

    if (index === 0) {
        this.head = this.head.next;
        return;
    }

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

    cur.next = cur.next.next;
};

 

猜你喜欢

转载自blog.csdn.net/weixin_42273637/article/details/87346372