forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move linked list traversals into separate section.
- Loading branch information
Showing
9 changed files
with
147 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Reversed Linked List Traversal | ||
|
||
The task is to traverse the given linked list in reversed order. | ||
|
||
For example for the following linked list: | ||
|
||
 | ||
|
||
The order of traversal should be: | ||
|
||
```text | ||
37 → 99 → 12 | ||
``` | ||
|
||
The time complexity is `O(n)` because we visit every node only once. | ||
|
||
## Reference | ||
|
||
- [Wikipedia](https://en.wikipedia.org/wiki/Linked_list) |
36 changes: 36 additions & 0 deletions
36
src/algorithms/linked-list/reverse-traversal/__test__/reverseTraversal.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import LinkedList from '../../../../data-structures/linked-list/LinkedList'; | ||
import reverseTraversal from '../reverseTraversal'; | ||
|
||
describe('reverseTraversal', () => { | ||
it('should traverse linked list in reverse order', () => { | ||
const linkedList = new LinkedList(); | ||
|
||
linkedList | ||
.append(1) | ||
.append(2) | ||
.append(3); | ||
|
||
const traversedNodeValues = []; | ||
const traversalCallback = (nodeValue) => { | ||
traversedNodeValues.push(nodeValue); | ||
}; | ||
|
||
reverseTraversal(linkedList, traversalCallback); | ||
|
||
expect(traversedNodeValues).toEqual([3, 2, 1]); | ||
}); | ||
}); | ||
|
||
|
||
// it('should reverse traversal the linked list with callback', () => { | ||
// const linkedList = new LinkedList(); | ||
// | ||
// linkedList | ||
// .append(1) | ||
// .append(2) | ||
// .append(3); | ||
// | ||
// expect(linkedList.toString()).toBe('1,2,3'); | ||
// expect(linkedList.reverseTraversal(linkedList.head, value => value * 2)).toEqual([6, 4, 2]); | ||
// expect(() => linkedList.reverseTraversal(linkedList.head)).toThrow(); | ||
// }); |
24 changes: 24 additions & 0 deletions
24
src/algorithms/linked-list/reverse-traversal/reverseTraversal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Traversal callback function. | ||
* @callback traversalCallback | ||
* @param {*} nodeValue | ||
*/ | ||
|
||
/** | ||
* @param {LinkedListNode} node | ||
* @param {traversalCallback} callback | ||
*/ | ||
function reverseTraversalRecursive(node, callback) { | ||
if (node) { | ||
reverseTraversalRecursive(node.next, callback); | ||
callback(node.value); | ||
} | ||
} | ||
|
||
/** | ||
* @param {LinkedList} linkedList | ||
* @param {traversalCallback} callback | ||
*/ | ||
export default function reverseTraversal(linkedList, callback) { | ||
reverseTraversalRecursive(linkedList.head, callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Linked List Traversal | ||
|
||
The task is to traverse the given linked list in straight order. | ||
|
||
For example for the following linked list: | ||
|
||
 | ||
|
||
The order of traversal should be: | ||
|
||
```text | ||
12 → 99 → 37 | ||
``` | ||
|
||
The time complexity is `O(n)` because we visit every node only once. | ||
|
||
## Reference | ||
|
||
- [Wikipedia](https://en.wikipedia.org/wiki/Linked_list) |
22 changes: 22 additions & 0 deletions
22
src/algorithms/linked-list/traversal/__test__/traversal.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import LinkedList from '../../../../data-structures/linked-list/LinkedList'; | ||
import traversal from '../traversal'; | ||
|
||
describe('traversal', () => { | ||
it('should traverse linked list', () => { | ||
const linkedList = new LinkedList(); | ||
|
||
linkedList | ||
.append(1) | ||
.append(2) | ||
.append(3); | ||
|
||
const traversedNodeValues = []; | ||
const traversalCallback = (nodeValue) => { | ||
traversedNodeValues.push(nodeValue); | ||
}; | ||
|
||
traversal(linkedList, traversalCallback); | ||
|
||
expect(traversedNodeValues).toEqual([1, 2, 3]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Traversal callback function. | ||
* @callback traversalCallback | ||
* @param {*} nodeValue | ||
*/ | ||
|
||
/** | ||
* @param {LinkedList} linkedList | ||
* @param {traversalCallback} callback | ||
*/ | ||
export default function traversal(linkedList, callback) { | ||
let currentNode = linkedList.head; | ||
|
||
while (currentNode) { | ||
callback(currentNode.value); | ||
currentNode = currentNode.next; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters