diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index e256b1fe..b2cf94f2 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -235,4 +235,21 @@ export default class LinkedList { 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 }; + }, + }; + } }