【发布时间】:2021-05-13 17:25:55
【问题描述】:
这是我迄今为止比较节点的代码:
const removeDuplicates = (headNode) => {
let cur = headNode;
while (cur.next) {
let nextnode = cur.next;
if (cur.data == nextnode.data) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return headNode;
}
如果列表是[1, 2, 1, 3, 4, 2, 1] => [1, 2, 3, 4]
第四个节点应该是 4 却得到了 1,为什么?
我该如何解决这个问题?
【问题讨论】:
-
类和函数没有关系
-
您使用 JavaScript
Set对象从数组中删除重复项,例如console.log([...new Set(arr)]);
标签: javascript class linked-list duplicates