-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
Conversation
"Reverse a linked list" is a popular algorithm challenge. It might be more appropriate to put these functions in a |
@trekhleb Could you share thoughts on this please? |
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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 Report
@@ Coverage Diff @@
## master #194 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 136 136
Lines 2505 2528 +23
Branches 416 419 +3
=====================================
+ Hits 2505 2528 +23
Continue to review full report at Codecov.
|
Hi @appleJax, |
Hi @hanhdt, thank you for PR! I would agree that linked list traversals are worthy to be put in new Meanwhile 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! |
* Add LinkedList traverse function * Add LinkedList reverse traversal implementations * Update LinkedList traverse function * Update LinkedList reverse traversal and test cases * Update LinkedList traversal tests
* Add LinkedList traverse function * Add LinkedList reverse traversal implementations * Update LinkedList traverse function * Update LinkedList reverse traversal and test cases * Update LinkedList traversal tests
Changes: