[Algorithm] Doubly Linked list construction
2022/8/4 6:22:57
本文主要是介绍[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;
这篇关于[Algorithm] Doubly Linked list construction的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-15SendGrid 的 Go 客户端库怎么实现同时向多个邮箱发送邮件?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么设置header 和 标签tag 呢?-icode9专业技术文章分享
- 2024-11-12Cargo deny安装指路
- 2024-11-02MongoDB项目实战:从入门到初级应用
- 2024-11-01随时随地一键转录,Google Cloud 新模型 Chirp 2 让语音识别更上一层楼
- 2024-10-25Google Cloud动手实验详解:如何在Cloud Run上开发无服务器应用
- 2024-10-24AI ?先驱齐聚 BAAI 2024,发布大规模语言、多模态、具身、生物计算以及 FlagOpen 2.0 等 AI 模型创新成果。
- 2024-10-20goland工具下,如修改一个项目的标准库SDK的版本-icode9专业技术文章分享
- 2024-10-17Go学习:初学者的简单教程
- 2024-10-17Go学习:新手入门完全指南