Design Linked List

题目描述:

Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement these functions in your linked list class:

  • get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
  • addAtHead(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.
  • addAtTail(val) : Append a node of value val to the last element of the linked list.
  • addAtIndex(index, val) : 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.
  • deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

Example:

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

Note:

  • All values will be in the range of [1, 1000].
  • The number of operations will be in the range of [1, 1000].
  • Please do not use the built-in LinkedList library.  

大概意思就是让你设计一个单链表(包含有初始化链表,桉位查找,结点插入,头插入,尾插入,删除结点)。代码如下:

/**
 * Initialize your data structure here.
 */

var MyLinkedList = function() {
    this.head = null;
    this.length = 0;
    //first=this.head;
    //first.next=null;
    //this.length=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>=this.length) return -1;
    let cur = this.head, i=0;
    while(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}z
 */

MyLinkedList.prototype.addAtHead = function(val) {
    //建立一个新的头结点newHead
    let newHead = {val:val}
    //改变原来的头结点的指向
    newHead.next = this.head;
    //新结点赋值给头结点
    this.head = newHead;
    this.length++;
};

/**
 * Append a node of value val to the last element of the linked list.
 * @param {number} val
 * @return {void}
 */

MyLinkedList.prototype.addAtTail = function(val) {
    if(this.length===0){
        this.addAtHead(val);
        return;
    }
    let cur = this.head;
    while(cur.next!=null) cur = cur.next;
    let newTail = {val:val};
    //将结点newTail插入终端结点
    cur.next = newTail;
    //链表建立完毕,将终端结点的指针域置空
    newTail.next = null;
    this.length++;
};

/**
 * 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>this.length) return -1;
    if(index===this.length){
        this.addAtTail(val);
        return;
    }
    let i=0, cur = this.head;
    //将工作指针cur后移
    while(i<index-1&&cur!=null) i++,  cur = cur.next;
    let newNode = {val:val};
    newNode.next = cur.next;
    //将结点newNode插入到cur后
    cur.next = newNode;
    this.length++;
};

/**
 * 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>=this.length) return -1;
    let i=0, cur = this.head;
    //查找第index-1个结点
    while(i<index-1&&cur!=null) i++, cur = cur.next;
    //暂存被删的节点
    let prev = cur.next;
    //改变结点指向
    cur.next = prev.next;
    this.length--;
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * var obj = Object.create(MyLinkedList).createNew()
 * var param_1 = obj.get(index)
 * obj.addAtHead(val)
 * obj.addAtTail(val)
 * obj.addAtIndex(index,val)
 * obj.deleteAtIndex(index)
 */

猜你喜欢

转载自blog.csdn.net/Jackson23333/article/details/81280459