leetcode.707 javascript实现链表(es5 & es6) | 刷题打卡

127 阅读4分钟

一、题目描述

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

示例:

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 库。

二、思路分析及代码实现

对于js来说,链表是一种全新的数据类型,我们首先应该先了解其定义才能去实现它。

单链表中的每个结点不仅包含值,还包含链接到下一个结点的引用字段。通过这种方式,单链表将所有结点按顺序组织起来。 image

1. 链表对象

如果将链表用js对象表示,那它应该具有的最基本的几个属性:

  • length 链表长度
  • start 头部节点/起始节点
var MyLinkedList = function () {
  let length = 0
  let start = null
};

目前MyLinkedList就是一个空的链表对象

2.初始化链表节点

因为js没有原生支持链表,所以创建一个节点我们也需要写个方法,把它new出来。一个链表节点能有什么东西呢?只有两个很简单的属性:

  • val 当前节点的数值
  • next 下一个节点指向(也可以理解为一个指针)
MyLinkedList.prototype.node = function (val) {
  this.val = val
  this.next = null
  this.prev = null
}

3.获取某一个链表节点的值

因为链表对象无法直接通过下标的方式获取(数组的第3位这种),链表要想获取第N位的值,只能从头部节点向后一个节点一个节点的查找直到找到对应的节点。

我们要实现的get方法就是传入一个索引,获取对应索引的链表节点的val。思路捋清楚~代码实现就相对简单了

MyLinkedList.prototype.get = function (index) {
  if (index < 0 || index >= this.length) return -1
  let current = this.start
  while (0 < index) {
    current = current.next
    index--
  }
  return current ? current.val : -1
}
  • 注意,对于索引位溢出的情况直接返回了-1

4.链表头部插入

要想给链表插入节点,首先需要new一个节点,传入该节点的val即可实现。关联之前的代码:

let tempNode = new this.node(val)

想要头部插入一个节点,思路很简单,首先将要插入的节点的next(下一节点)指向当前链表的起始节点,然后再将当前链表的起始节点指向插入的头部节点,最后将链表的length++即可实现。

MyLinkedList.prototype.addAtHead = function (val) {
  let tempNode = new this.node(val)
  tempNode.next = this.start || null
  this.start = tempNode
  this.length++
}

5.链表尾部插入

和头部插入节点很像,我们只需要多一步操作:找到最后一个节点即可。

通过判断节点是否含有next节点可以判断当前节点是不是最后一位,如果是最后一位直接插入到最后一位节点之后即可。

MyLinkedList.prototype.addAtTail = function (val) {
  let tailNode = new this.node(val)
  let tempNode = this.start
  while (tempNode && tempNode.next) {
    tempNode = tempNode.next
  }
  if (tempNode) {
    tempNode.next = tailNode
  } else {
    tempNode = tailNode
    this.start = tailNode
  }

  this.length++
};

6.链表任意插入

在这里引用leetcode上的三张图展示流程应该是最直观的

  • 1.初始化节点cur image

  • 2.将 cur 的 next 字段链接到 prev 的下一个结点 next image

  • 3.将 prev 中的 next 字段链接到 cur 。 image

参考以上的插入流程再看接下来的代码应该能更好地理解。

MyLinkedList.prototype.addAtIndex = function (index, val) {
  if (index <= 0) return this.addAtHead(val)
  if (index === this.length) return this.addAtTail(val)
  if (index > this.length) return

  let addNode = new this.node(val)
  let cur = this.start

  while (index-- > 1) {
    cur = cur.next
  }
  addNode.next = cur.next
  cur.next = addNode
  this.length++
};

7.链表任意位置删除

  • 1.找到 cur 的上一个结点 prev 及其下一个结点 next ; image

  • 2.接下来链接 prev 到 cur 的下一个节点 next 。 image

MyLinkedList.prototype.deleteAtIndex = function (index) {
  if (index < 0 || index >= this.length) return
  if (index) {
    let i = 1
    let cur = this.start
    while ((index - 1) >= i) {
      cur = cur.next
      i++
    }
    if (cur && cur.next) {
      cur.next = cur.next.next
    }
    this.length--

  } else {
    this.start = this.start.next
    this.length--
  }
}

8.总结

  • 链表属性
    • length 链表长度
    • start 起始节点
  • 节点属性
    • val 节点数值
    • next 指向的下一个节点
    • prve 指向当前节点的上一个节点(单链表时不存在)
  • 方法
    • get 获取对应索引的节点数值 (二、3)
    • addAtHead 头部插入节点 (二、4)
    • addAtTail 尾部插入节点 (二、5)
    • addAtIndex 索引位置插入 (二、6)
    • deleteAtIndex 索引位置删除 (二、7)

三、完整实现

1.es5 原型链版本

var MyLinkedList = function () {
  let length = 0
  let start = null
};

MyLinkedList.prototype.node = function (val) {
  this.val = val
  this.next = null
  this.prev = null
  return this
}

MyLinkedList.prototype.get = function (index) {
  if (index < 0 || index >= this.length) return -1
  let current = this.start
  while (0 < index) {
    current = current.next
    index--
  }
  return current ? current.val : -1
}

MyLinkedList.prototype.addAtHead = function (val) {
  let tempNode = new this.node(val)
  tempNode.next = this.start || null
  this.start = tempNode
  this.length = this.length ? ++this.length : 1
};

MyLinkedList.prototype.addAtTail = function (val) {
  let tailNode = new this.node(val)
  let tempNode = this.start
  while (tempNode && tempNode.next) {
    tempNode = tempNode.next
  }
  if (tempNode) {
    tempNode.next = tailNode
  } else {
    tempNode = tailNode
    this.start = tailNode
  }

  this.length = this.length ? ++this.length : 1
};

MyLinkedList.prototype.addAtIndex = function (index, val) {
  if (index <= 0) return this.addAtHead(val)
  if (index === this.length) return this.addAtTail(val)
  if (index > this.length) return

  let addNode = new this.node(val)
  let cur = this.start

  while (index-- > 1) {
    cur = cur.next
  }
  addNode.next = cur.next
  cur.next = addNode
  this.length++
};

MyLinkedList.prototype.deleteAtIndex = function (index) {
  if (index < 0 || index >= this.length) return
  if (index) {
    let i = 1
    let cur = this.start
    while ((index - 1) >= i) {
      cur = cur.next
      i++
    }
    if (cur && cur.next) {
      cur.next = cur.next.next
    }
    this.length--

  } else {
    this.start = this.start.next
    this.length--
  }
};

2.es6 Class版本

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

class MyLinkedList {
  constructor() {
    this.start = null
    this.length = 0
  }

  get(index) {
    if (index < 0 || index >= this.length) return -1
    let current = this.start
    while (0 < index) {
      current = current.next
      index--
    }
    return current ? current.val : -1
  }

  addAtHead(val) {
    let tempNode = new Node(val)
    tempNode.next = this.start || null
    this.start = tempNode
    this.length = this.length ? ++this.length : 1
  }

  addAtTail(val) {
    let tailNode = new Node(val)
    let tempNode = this.start
    while (tempNode && tempNode.next) {
      tempNode = tempNode.next
    }
    if (tempNode) {
      tempNode.next = tailNode
    } else {
      tempNode = tailNode
      this.start = tailNode
    }

    this.length = this.length ? ++this.length : 1
  }

  addAtIndex(index, val) {
    if (index <= 0) return this.addAtHead(val)
    if (index === this.length) return this.addAtTail(val)
    if (index > this.length) return

    let addNode = new Node(val)
    let cur = this.start

    while (index-- > 1) {
      cur = cur.next
    }
    addNode.next = cur.next
    cur.next = addNode
    this.length++
  }

  deleteAtIndex(index) {
    if (index < 0 || index >= this.length) return
    if (index) {
      let i = 1
      let cur = this.start
      while ((index - 1) >= i) {
        cur = cur.next
        i++
      }
      if (cur && cur.next) {
        cur.next = cur.next.next
      }
      this.length--

    } else {
      this.start = this.start.next
      this.length--
    }
  }

}

END


本文正在参与「掘金 2021 春招闯关活动」, 点击查看 活动详情