-
Notifications
You must be signed in to change notification settings - Fork 31.1k
Open
Description
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)
} martin190578, willow-iam and Shiva953martin190578 and hassni12
Metadata
Metadata
Assignees
Labels
No labels