Make the linked list an iterable

This commit is contained in:
Aonghus O Nia 2020-03-06 22:30:12 -05:00 committed by GitHub
parent ba2d8dc4a8
commit 218a9c3643
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -235,4 +235,21 @@ export default class LinkedList {
return this; return this;
} }
/**
* make the linked list iterable
* @return {iterator}
*/
[Symbol.iterator]() {
let currentNode = this.head;
return {
next: () => {
if (!currentNode) return { done: true };
const { value, next } = currentNode;
currentNode = next;
return { value, done: false };
},
};
}
} }