707.设计链表

768 阅读2分钟

设计链表

总结

  • 书写题目的时候,需要先声明链表,再声明节点。
  • 并且length是在你每次进行操作的时候,才++,不需要声明一个函数来遍历
  • 调用声明在prototype上的方法的时候,需要使用this.方法名来调用。
  • ⚠️链表为空情况
    • 在尾部插入,相当于在首部插入
  • 插入到index位置的元素,倒着判断时(找上一个元素),需要注意,如果是在index = 0插入,就又回到了在链表首部插入的解法
  • index--的位置,应该在每次判断当前index是否符合之后,再--
  • 要仔细检查,自己书写的h.next会不会出错,是否不会涉及到null

代码

// 定义单链表,针对单链表进行操作
var MyLinkedList = function() {
  this.head = null;
  this.tail = null;
  this.length = 0;
};
// 定义单链表上的每一个节点
var listNode = function(val) {
  this.val = val
  this.next = null
}

/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.get = function(index) {
  // 保存当前的头节点
  let h = this.head;
  // 传递的为空
  // 无效的index,也需要返回
  if (!h || index < 0 || index > this.length) {
    return -1;
  }
  // 循环判断,get获取的数据
  while (h) {
    // console.log('get h', h,'index',index);
    if (index == 0) {
      return h.val;
    }
    index--;
    h = h.next;
  }
  return -1;
};

/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function(val) {
  // 添加到头部
  // 创建一个新的节点
  let node = new listNode(val);
  // 将指针指向之前的头节点
  node.next = this.head;
  this.head = node;
  this.length++;
  // console.log('添加头',this.head,this.length);
};

/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
  // 添加节点到末尾
  // 创建新的节点
  let node = new listNode(val);
  // 获取最后一个节点
  // 记录下头节点
  let h = this.head;
  // 添加的为空链表
  if (!h) {
    this.head = node;
    this.length++;
  }else {
    while (h.next != null) {
      // 循环结束后,当前的h就是最后一个节点
      h = h.next;
    }
    h.next = node;
    node.next = null;
    this.length++;
    // console.log('添加尾',this.head);
  }
};

/**
* @param {number} index
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function(index, val) {
  // 创建新的需要添加的节点
  let node = new listNode(val);
  // console.log('当前len',this.length);
  // index等于链表长度,插末尾
  if (index <= 0) {
    // index小于0,插头部
    this.addAtHead(val);
  }else {
    // console.log('l',this.length,this.head);
    if (index == this.length) {
    this.addAtTail(val);
  } else if (index < this.length) {
    // 小于链表的长度,需要进行添加操作
    // 在第index个节点之前添加
    let h = this.head;
    // console.log('tou',this.head);
    while (h != null) {
      if (index == 1) {
      // 找到需要插入位置的前一个节点
      node.next = h.next;
      h.next = node;
      this.length++;
      // console.log('h',this.head);
      break;
    }
    index--;
    h = h.next;
    // console.log('插入index',index,h);
  }
 }
}
  // console.log('hhhh',this.head);
  // index大于,不插
};

/**
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function(index) {
  // 判断index是否有效
  if (index >= 0 && index < this.length) {
    // console.log('删除',this.length);
    // 删除链表中的节点
    let tempt = null;
    let h = this.head;
    // 删除头节点
    if (index == 0) {
      this.head = this.head.next;
    }
  // 删除的非头节点
  while (h != null) {
    if (index == 1) {
      // 找到当前节点,进行操作
      tempt = h.next;
      h.next = h.next.next;
      tempt.next = null;
      this.length--;
      break;
    }
  index--;
  h = h.next;
  }
 }
};

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

附录