Add iterator support to traverse a linked list in JavaScript

This commit is contained in:
Shiva953 2023-04-20 17:38:12 +05:30
parent 8c5e5f4f0d
commit ba35037c17

View File

@ -9,10 +9,9 @@
* @param {traversalCallback} callback
*/
export default function traversal(linkedList, callback) {
let currentNode = linkedList.head;
const iterator = linkedList.getIterator();
while (currentNode) {
callback(currentNode.value);
currentNode = currentNode.next;
for (const nodeValue of iterator) {
callback(nodeValue);
}
}