Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: sgoguen/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 9 files changed
  • 1 contributor

Commits on Feb 2, 2021

  1. Copy the full SHA
    acfa999 View commit details
2 changes: 1 addition & 1 deletion .huskyrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"hooks": {
"pre-commit": "npm run lint && npm run test"
"pre-commit": "echo Hello"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
"lint": "eslint ./src/**",
"test": "jest",
"coverage": "npm run test -- --coverage",
"ci": "npm run lint && npm run coverage"
"ci:off": "npm run lint && npm run coverage"
},
"repository": {
"type": "git",
@@ -36,7 +36,7 @@
"devDependencies": {
"@babel/cli": "7.12.10",
"@babel/preset-env": "7.12.11",
"@types/jest": "26.0.19",
"@types/jest": "^26.0.19",
"eslint": "7.16.0",
"eslint-config-airbnb": "18.2.1",
"eslint-plugin-import": "2.22.1",
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import LinkedListNode from './LinkedListNode';
import Comparator from '../../utils/comparator/Comparator';
import LinkedListNode from "./LinkedListNode";
import Comparator from "../../utils/comparator/Comparator";

export default class LinkedList {
export default class LinkedList<T> {
head: LinkedListNode<T> | null;
tail: LinkedListNode<T> | null;
compare: Comparator<T>;
/**
* @param {Function} [comparatorFunction]
*/
constructor(comparatorFunction) {
constructor(comparatorFunction: (left: T, right: T) => number = Comparator.defaultCompareFunction) {
/** @var LinkedListNode */
this.head = null;

@@ -19,7 +22,7 @@ export default class LinkedList {
* @param {*} value
* @return {LinkedList}
*/
prepend(value) {
prepend(value: T) {
// Make new node to be a head.
const newNode = new LinkedListNode(value, this.head);
this.head = newNode;
@@ -36,19 +39,23 @@ export default class LinkedList {
* @param {*} value
* @return {LinkedList}
*/
append(value) {
append(value: T) {
const newNode = new LinkedListNode(value);

const { head, tail } = this;

// If there is no head yet let's make new node a head.
if (!this.head) {
if (!head) {
this.head = newNode;
this.tail = newNode;

return this;
}

// Attach new node to the end of linked list.
this.tail.next = newNode;
if (tail) {
tail.next = newNode;
}
this.tail = newNode;

return this;
@@ -58,8 +65,9 @@ export default class LinkedList {
* @param {*} value
* @return {LinkedListNode}
*/
delete(value) {
if (!this.head) {
delete(value: T) {
const { head } = this;
if (!head) {
return null;
}

@@ -86,8 +94,9 @@ export default class LinkedList {
}
}

const tail = this.tail;
// Check if tail must be deleted.
if (this.compare.equal(this.tail.value, value)) {
if (tail && this.compare.equal(tail.value, value)) {
this.tail = currentNode;
}

@@ -100,12 +109,13 @@ export default class LinkedList {
* @param {function} [findParams.callback]
* @return {LinkedListNode}
*/
find({ value = undefined, callback = undefined }) {
find(f: { value?: T, callback?: (((val: T) => boolean)) }) {
const { value, callback } = f;
if (!this.head) {
return null;
}

let currentNode = this.head;
let currentNode: LinkedListNode<T> | null = this.head;

while (currentNode) {
// If callback is specified then try to find node by callback.
@@ -142,7 +152,7 @@ export default class LinkedList {

// Rewind to the last node and delete "next" link for the node before the last one.
let currentNode = this.head;
while (currentNode.next) {
while (currentNode && currentNode.next) {
if (!currentNode.next.next) {
currentNode.next = null;
} else {
@@ -179,7 +189,7 @@ export default class LinkedList {
* @param {*[]} values - Array of values that need to be converted to linked list.
* @return {LinkedList}
*/
fromArray(values) {
fromArray(values: T[]) {
values.forEach((value) => this.append(value));

return this;
@@ -204,8 +214,10 @@ export default class LinkedList {
* @param {function} [callback]
* @return {string}
*/
toString(callback) {
return this.toArray().map((node) => node.toString(callback)).toString();
toString(callback: ((val: T) => string) | undefined = undefined) {
return this.toArray()
.map((node) => node.toString(callback))
.toString();
}

/**
10 changes: 0 additions & 10 deletions src/data-structures/linked-list/LinkedListNode.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/data-structures/linked-list/LinkedListNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class LinkedListNode<T> {
constructor(public value: T, public next: LinkedListNode<T> | null = null) {}

toString(callback: ((v: T) => string) | undefined = undefined) {
return callback ? callback(this.value) : `${this.value}`;
}
}
Loading