【路飞】算法与数据结构-设计链表  

115 阅读2分钟

LeetCode:地址

题目要求

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性: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 个节点。

示例 1:

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

提示:

  • 所有val值都在 [1, 1000] 之内。
  • 操作次数将在  [1, 1000] 之内。
  • 请不要使用内置的 LinkedList 库。
思路

以最朴实的方式实现的列表,易于分析理解;

代码

单链表

//初始化
var MyLinkedList = function () {
  //节点有一个val和一个next代表该节点的值和下一节点
  this.Node = function (val) {
    this.val = val;
    this.next = null;
  };
  //此处没有设置虚拟头节点,大家可以自己试试,有虚拟头的话,后面遍历链表的话会简单的多。
  this.size = 0;
};

//获取操作
MyLinkedList.prototype.get = function (index) {
  //判断边界
  if (index < 0 || index > this.size - 1) {
    return -1;
  }
  //从头开始遍历,其他方法同理
  let node = this.head;
  //查找index
  for (let i = 0; i < index; i++) {
    node = node.next;
  }
  return node.val;
};

//头尾添加节点均调用了addAtindex的方法
MyLinkedList.prototype.addAtHead = function (val) {
  this.addAtIndex(0, val);
};

MyLinkedList.prototype.addAtTail = function (val) {
  this.addAtIndex(this.size, val);
};

//添加操作
MyLinkedList.prototype.addAtIndex = function (index, val) {
  let node = this.head;
  //判断小于等于0和链表长度为0的情况
  if (index <= 0 || this.size === 0) {
    this.head = new this.Node(val);
    this.head.next = node;
  } else {
    //当index > 0时
    for (let i = 0; i < index - 1 && i <= this.size; i++) {
      node = node.next;
    }
    //循环结束开始插入
    let temp = node.next;
    node.next = new this.Node(val);
    node.next.next = temp;
  }
  this.size++;
  return;
};

//删除操作
MyLinkedList.prototype.deleteAtIndex = function (index) {
  if (index < 0 || index > this.size - 1) {
    return -1;
  }

  let node = this.head;

  if (index === 0) {
    //对0单独判断
    this.head = this.head.next;
    //分为大于小于0两种情况
  } else {
    //查找index
    for (let i = 0; i < index - 1; i++) {
      node = node.next;
    }
    // a --> b --> c  a --> c
    let temp = node.next;
    node.next = temp.next;
    temp = null;
  }
  
  this.size--;
  return;
};

双链表

var MyLinkedList = function () {
  this.Node = function (val, prev, next) {
    this.next = next;
    this.prev = prev;
    this.val = val;
  };
  //这里创建了一个虚拟头
  this.dummyHead = new this.Node();
  this.size = 0;
};

//get和add的遍历都是遍历到目标节点的前一个元素
MyLinkedList.prototype.get = function (index) {
  if (index > this.size - 1 || index < 0) {
    return -1;
  }
  let node = this.dummyHead;
  for (let i = 0; i < index; i++) {
    node = node.next;
  }
  return node.next.val;
};

MyLinkedList.prototype.addAtHead = function (val) {
  this.addAtIndex(0, val);
};

MyLinkedList.prototype.addAtTail = function (val) {
  this.addAtIndex(this.size, val);
};