javascript-algorithms icon indicating copy to clipboard operation
javascript-algorithms copied to clipboard

feat:linked list add new method

Open lxhyl opened this issue 3 years ago • 0 comments

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)
} 

lxhyl avatar Jan 10 '22 14:01 lxhyl