因此,我的初始代码如下所示:

class LinkedList {
  constructor() {
    this.head = new Node(-1);
    this.tail = this.head;
  };
  prepend(value) {
    let next = this.head.nextNode;
    this.head = new Node(value);
    this.head.nextNode = next;
  };
};

class Node {
  constructor(value, nextNode = null) {
    this.value = value;
    this.nextNode = nextNode;
  };
};

假设我在前面加上一个值1,然后再加上一个值2.输出将如下所示:

LinkedList = {
  head: {
    nextNode: null,
    value: 2
  },
  tail: {
    nextNode: null,
    value: -1
  }
};

这真的不是我想要的.相反,我希望Head的值等于2,然后让Head指向一个值为1的新 node .

我完全不知道如何解决这个问题,而且我不想只查找答案,因为在第一次学习了这个概念后,我正在try 学习如何自己实现这样的东西.如果我能得到任何帮助,我将不胜感激.我更希望你能给我一个提示,让我朝着正确的方向前进,然后把正确的方法放在下面来解决这个问题,因为我仍然决心在不查找答案的情况下解决这个问题.提前谢谢!

推荐答案

如果您将某个伪 node 用于空列表,则应在添加真实 node 之前将其清除.

对于下一个 node ,您应该使用this.head而不是this.head.nextNode(初始为空).

此外,您没有使用Node的构造函数的第二个参数--将下一个 node 直接传递给构造函数:

class LinkedList {
  constructor() {
    this.tail = this.head = new Node(-1);
  };
  prepend(value) {
    if(this.head.value === -1 && this.head.nextNode === null){ // an empty list, delete the head and tail
      delete this.head, delete this.tail;
    }
    this.head = new Node(value, this.head); // leverage the Node's constructor to pass the next node
    this.tail ??= this.head; // set the tail on the first node
  };
};

class Node {
  constructor(value, nextNode = null) {
    this.value = value;
    this.nextNode = nextNode;
  };
};

const list = new LinkedList;

list.prepend(1);
list.prepend(2);

console.log(JSON.stringify(list, null, 4));
.as-console-wrapper { top: 0; max-height: 100% !important; }

我建议将-1改为一个符号:

class LinkedList {
  static EMPTY_NODE = Symbol();
  constructor() {
    this.tail = this.head = new Node(LinkedList.EMPTY_NODE);
  };
  prepend(value) {
    if(this.head.value === LinkedList.EMPTY_NODE){
      delete this.head, delete this.tail;
    }
    this.head = new Node(value, this.head);
    this.tail ??= this.head;
  };
};

class Node {
  constructor(value, nextNode = null) {
    this.value = value;
    this.nextNode = nextNode;
  };
};

const list = new LinkedList;

list.prepend(1);
list.prepend(2);

console.log(JSON.stringify(list, null, 4));
.as-console-wrapper { top: 0; max-height: 100% !important; }

但对于一个空的列表,我更希望头部和尾部是null:

class LinkedList {
  constructor() {
    this.tail = this.head = null;
  };
  prepend(value) {
    this.head = new Node(value, this.head);
    this.tail ??= this.head;
  };
};

class Node {
  constructor(value, nextNode = null) {
    this.value = value;
    this.nextNode = nextNode;
  };
};

const list = new LinkedList;

list.prepend(1);
list.prepend(2);

console.log(JSON.stringify(list, null, 4));
.as-console-wrapper { top: 0; max-height: 100% !important; }

Javascript相关问答推荐

获取加载失败:获取[.]添加时try 将文档添加到Firerestore,Nuxt 3

React:未调用useState变量在调试器的事件处理程序中不可用

手机上的渲染错误文本必须在文本组件中渲染,但在浏览器上没有问题<><>

如何在bslib nav_insert之后更改导航标签的CSS类和样式?

如何使onPaste事件与可拖动的HTML元素一起工作?

如何使用子字符串在数组中搜索重复项

为什么123[';toString';].long返回1?

正则表达式,允许我匹配除已定义的子字符串之外的所有内容

Eval vs函数()返回语义

AJAX POST在控制器中返回空(ASP.NET MVC)

将范围 Select 器添加到HighChart面积图

select 2-删除js插入的项目将其保留为选项

为什么在运行于<;img>;事件处理程序中的JavaScript中x和y是不可变的?

如何在TransformControls模式下只保留箭头进行翻译?

JavaScript:多个图像错误处理程序

当S点击按钮时,我如何才能改变它的样式?

在执行console.log(new X())时记录一个字符串

浮动标签效果移除时,所需的也被移除

如何动态呈现适合未知屏幕大小的最大数量的表行?苗条的

使用重新 Select 和对象理解 Select 器备忘