# 双链表

简介 - 双链表 (opens new window)

# 设计链表

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:valnextval 是当前节点的值,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 个节点。
/**
 * Initialize your data structure here.
 */
var MyLinkedList = function() {
  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 - 1) {
    return -1;
  }
  let curr = this.head;
  while (index) {
    curr = curr.next;
    index--;
  }
  return curr ? curr.val : -1;
};

/**
 * 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) {
  var newHead = {
    prev: null,
    val,
    next: this.head
  };
  if (this.head) {
    this.head.prev = newHead;
  }
  this.head = newHead;
  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) {
  if (!this.head) {
    this.head = {
      prev: null,
      val,
      next: null
    };
  } else {
    var tail = this.head;
    var count = this.size - 1;
    while (count) {
      tail = tail.next;
      count--;
    }
    var newTail = {
      prev: tail,
      val,
      next: null
    };
    tail.next = newTail;
  }
  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 > this.size) {
    return false;
  }
  if (index <= 0) {
    this.addAtHead(val);
  } else if (index === this.size) {
    this.addAtTail(val);
  } else {
    var curr = this.head;
    while (index) {
      curr = curr.next;
      index--;
    }
    var currPrev = curr.prev;
    var newNode = {
      prev: currPrev,
      val,
      next: curr
    };
    currPrev.next = newNode;
    curr.prev = newNode;
    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 - 1) {
    return false;
  }
  if (index === 0) {
    this.head = this.head.next;
    if (this.head) {
      this.head.prev = null;
    }
    this.size--;
    return this.head;
  }
  var curr = this.head;
  while (index) {
    curr = curr.next;
    index--;
  }
  var currPrev = curr.prev;
  var currNext = curr.next;
  currPrev.next = currNext;
  if (currNext) {
    currNext.prev = currPrev;
  }
  this.size--;
  return this.head;
};

/**
 * 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)
 */

作者:力扣 (LeetCode)
链接:https://leetcode-cn.com/leetbook/read/linked-list/fabl3/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。