[Algorithm] Doubly Linked list construction
// This is an input class. Do not edit.
class Node {
constructor(value) {
this.value = value;
this.prev = null;
this.next = null;
}
}
// Feel free to add new properties and methods to the class.
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
}
setHead(node) {
// node is the only node
if (!this.head) {
this.head = node
this.tail = node
} else {
this.insertBefore(this.head, node);
}
}
setTail(node) {
if (!this.head) {
this.setHead(node)
} else {
this.insertAfter(this.tail, node)
}
}
insertBefore(node, nodeToInsert) {
// only one node
if (nodeToInsert === this.head && nodeToInsert === this.tail) {
return;
}
this.remove(nodeToInsert);
nodeToInsert.prev = node.prev;
nodeToInsert.next = node;
if (!node.prev) {
this.head = nodeToInsert
} else {
node.prev.next = nodeToInsert
}
node.prev = nodeToInsert
}
insertAfter(node, nodeToInsert) {
// only one node
if (nodeToInsert === this.head && nodeToInsert === this.tail) {
return;
}
this.remove(nodeToInsert);
nodeToInsert.prev = node;
nodeToInsert.next = node.next;
if (!node.next) {
this.tail = nodeToInsert
} else {
node.next.prev = nodeToInsert
}
node.next = nodeToInsert
}
insertAtPosition(position, nodeToInsert) {
// if position = 1
if (this.position === 1) {
this.setHead(nodeToInsert)
} else {
let current = this.head;
let currentPosition = 1;
while (current !== null && currentPosition !== position) {
current = current.next;
currentPosition++
}
if (current === null) {
this.setTail(nodeToInsert)
}
if (currentPosition === position) {
this.insertBefore(current, nodeToInsert)
}
}
}
removeNodesWithValue(value) {
let current = this.head;
while(current) {
// set it earlier
const nodeToRemove = current;
current = current.next;
if (nodeToRemove.value === value) {
this.remove(nodeToRemove)
}
}
}
remove(node) {
// check head
if (this.head === node) {
this.head = this.head.next;
}
if (this.tail === node) {
// check tail
this.tail = this.tail.prev;
}
this.removeNodeBindings(node)
}
removeNodeBindings(node) {
const prevNode = node.prev
const nextNode = node.next
if (prevNode) {
prevNode.next = nextNode
}
if(nextNode) {
nextNode.prev = prevNode
}
node.prev = null;
node.next = null;
}
containsNodeWithValue(value) {
let current = this.head;
while(current !== null && current.value !== value) {
current = current.next;
}
return current !== null;
}
}
// Do not edit the lines below.
exports.Node = Node;
exports.DoublyLinkedList = DoublyLinkedList;
分类:
Algorithms
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2021-08-04 [SAA + SAP] 19. Database & Redshfit
2021-08-04 [SAA + SAP] 18. Architecture Discussions - 1
2020-08-04 [AWS] Build an App with AWS CDK
2019-08-04 [React Native] Up & Running with React Native & TypeScript
2019-08-04 [React] Create a Query Parameter Modal Route with React Router
2018-08-04 [JavaEE] Implement a test for REST endpoint
2018-08-04 [JavaEE] Implement a REST Endpoint