MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / remove

Method remove

Data-Structures/Linked-List/SinglyLinkedList.js:121–144  ·  view source on GitHub ↗
(element)

Source from the content-addressed store, hash-verified

119
120 // Removes the node with the value as param
121 remove(element) {
122 if (this.isEmpty()) return null
123 let { currentNode } = this.initiateNodeAndIndex()
124 let removedNode = null
125 // Check if the head node is the element to remove
126 if (currentNode.data === element) {
127 return this.removeFirst()
128 }
129 // Check if the tail node is the element to remove
130 if (this.tailNode.data === element) {
131 return this.removeLast()
132 }
133 // Check which node is the node to remove
134 while (currentNode.next) {
135 if (currentNode.next.data === element) {
136 removedNode = currentNode.next
137 currentNode.next = currentNode.next.next
138 this.length--
139 return removedNode.data
140 }
141 currentNode = currentNode.next
142 }
143 return removedNode?.data || null
144 }
145
146 // Returns the index of the element passed as param otherwise -1
147 indexOf(element) {

Calls 4

isEmptyMethod · 0.95
initiateNodeAndIndexMethod · 0.95
removeFirstMethod · 0.95
removeLastMethod · 0.95

Tested by

no test coverage detected