trekhleb / trekhleb/javascript-algorithms

feat:linked list add new method

Open
#832 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
197k
Forks
31k
PR merge metrics
No merged PRs in 30d

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no implementation file or test. Locate the linked-list implementation and its existing traversal-related tests, then review how iteration is currently exposed. Done means the linked list supports the proposed iterator and for-of traversal, with behavior verified for populated and empty lists.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.