mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
Add iterator support to traverse a linked list in JavaScript
This commit is contained in:
parent
8c5e5f4f0d
commit
ba35037c17
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user