mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Make the linked list an iterable
This commit is contained in:
parent
ba2d8dc4a8
commit
218a9c3643
@ -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 };
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user