Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LinkedList traversal and reverse implementations #194

Merged
merged 7 commits into from
Sep 8, 2018

Conversation

hanhdt
Copy link
Contributor

@hanhdt hanhdt commented Aug 31, 2018

Changes:

  • Add LinkedList traverse implementation
  • Add LinkedList reverse implementations (reverseTraversal and reverse)
  • Update their pseudocode

@appleJax
Copy link
Contributor

"Reverse a linked list" is a popular algorithm challenge. It might be more appropriate to put these functions in a algorithms/linked-list folder? @trekhleb thoughts?

@hanhdt
Copy link
Contributor Author

hanhdt commented Sep 4, 2018

@trekhleb Could you share thoughts on this please?

Copy link
Contributor

@appleJax appleJax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are the changes I would make to the functions. The advice should be relevant whether the functions end up being separate algorithms or methods on LinkedList.

/**
* The items in the list have been traversed in reverse order
*/
reverseTraversal(callback = undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation of reverseTraversal has a running time of O(n^2). It would be more straightforward and more performant (O(n)) to just define reverseTraversal as traverse().reverse().

If you want to write a reverseTraverse function as an algorithm exercise, it might be better to define it recursively, like this:

function reverseTraverse(node, callback) {
  if (typeof callback !== 'function') {
    throw new TypeError(
      `reverseTraverse method requires a callback function as an argument.\nArgument given: ${typeof callback}`
    )
  }

  if (!node) return [];

  return  reverseTraverse(
    node.next, callback
  ).concat(callback(node.value));
}

.append(2)
.append(3);

const traversedNodes = linkedList.traverse();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linkedList.traverse() should be passed the callback. Also, it should be passed a callback that changes the values, to distinguish it from toArray(). e.g. const callback = (x) => x * 2;

expect(traversedNodeValues).toEqual([1, 2, 3]);
});

it('should traverse through all nodes with callback', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you adopt the other changes suggested, this test can be deleted.

@codecov
Copy link

codecov bot commented Sep 5, 2018

Codecov Report

Merging #194 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #194   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         136    136           
  Lines        2505   2528   +23     
  Branches      416    419    +3     
=====================================
+ Hits         2505   2528   +23
Impacted Files Coverage Δ
src/data-structures/linked-list/LinkedList.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d038c40...fc812d9. Read the comment docs.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@hanhdt
Copy link
Contributor Author

hanhdt commented Sep 8, 2018

Hi @appleJax,
Could you help me review them again?
Thanks.

@trekhleb trekhleb changed the base branch from master to linked-list-methods September 8, 2018 08:50
@trekhleb
Copy link
Owner

trekhleb commented Sep 8, 2018

Hi @hanhdt, thank you for PR!
@appleJax thank you reviews as well!

I would agree that linked list traversals are worthy to be put in new algorithms/linked-list section. This way we will do the same is we do with trees and graphs which have their BFS and DFS traversals as a separate algorithms.

Meanwhile reverse() method may be a good fit for LinkedList class itself.

So I'm going to merge these changes for now and I'll move traversal codes to new section.

Thank you for your effort guys!

@trekhleb trekhleb merged commit 4989a6a into trekhleb:linked-list-methods Sep 8, 2018
harshes53 pushed a commit to harshes53/javascript-algorithms that referenced this pull request Dec 6, 2018
* Add LinkedList traverse function

* Add LinkedList reverse traversal implementations

* Update LinkedList traverse function

* Update LinkedList reverse traversal and test cases

* Update LinkedList traversal tests
shoredata pushed a commit to shoredata/javascript-algorithms that referenced this pull request Mar 28, 2019
* Add LinkedList traverse function

* Add LinkedList reverse traversal implementations

* Update LinkedList traverse function

* Update LinkedList reverse traversal and test cases

* Update LinkedList traversal tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants