Move linked list traversals into separate section.

This commit is contained in:
Oleksii Trekhleb
2018-09-08 22:20:52 +03:00
parent 2feec48ea6
commit 80ecbe0b3e
9 changed files with 147 additions and 60 deletions

View File

@@ -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:
![](https://upload.wikimedia.org/wikipedia/commons/6/6d/Singly-linked-list.svg)
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)

View File

@@ -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();
// });

View File

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

View File

@@ -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:
![](https://upload.wikimedia.org/wikipedia/commons/6/6d/Singly-linked-list.svg)
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)

View File

@@ -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]);
});
});

View File

@@ -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;
}
}