javascript-algorithms
javascript-algorithms copied to clipboard
feat:linked list add new method
traverse linked list is easy but uninteresting. You must add new variable and use while.So why not add a method to return a iterator.Then we can use for of to traverse. eg:
getIterator() {
const _this = this
return {
*[Symbol.iterator]() {
let currentNode = _this.head
if (!currentNode) yield node
while (currentNode) {
yield currentNode.value
currentNode = currentNode.next
}
}
}
}
use
const iter = linkedList.getIterator()
for(const item of iter){
console.log(item)
}