小编Elc*_*hin的帖子

JavaScript 中带尾部的 LinkedList Push() 方法

我尝试了解push()方法如何与 JS 中的 tails 一起使用。这是代码:

class Node {
    constructor(val) {
      this.val = val;
      this.next = null;
    }
  }
  class SinglyLinkedList {
    constructor() {
      this.length = 0;
      this.head = null;
      this.tail = null;
    }
    push(val) {
      const newNode = new Node(val)
      if (this.head===null) { // happens only once
        this.head = newNode;
        this.tail = this.head;
      } else {
        this.tail.next = newNode;   // this.a.next = b node???
        this.tail = newNode;
      }
      this.length++
    }
Run Code Online (Sandbox Code Playgroud)

具体来说,我不明白else该方法内部的部分push()。如果我们说,如何为每个nextahead …

javascript linked-list data-structures singly-linked-list

6
推荐指数
1
解决办法
1503
查看次数