Skip to content

Instantly share code, notes, and snippets.

@shovon
Created August 5, 2023 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shovon/56cd0c8fbf9e60c36bdf39921accd638 to your computer and use it in GitHub Desktop.
Save shovon/56cd0c8fbf9e60c36bdf39921accd638 to your computer and use it in GitHub Desktop.
A sortable link list

Sortable Linked List

Usage

class ComparableNumber implements Comparable<number> {
	constructor(private _value: number) {}

	get value() {
		return this._value;
	}

	compare(b: number): number {
		return this._value - b;
	}
}

const l = new LLNode(new ComparableNumber(5));
l.insert(new ComparableNumber(4));
l.insert(new ComparableNumber(3));
l.insert(new ComparableNumber(10));
l.insert(new ComparableNumber(8));
l.insert(new ComparableNumber(2));

function* map<T, V>(iterable: Iterable<T>, fn: (item: T) => V): Iterable<V> {
	for (const value of iterable) {
		yield fn(value);
	}
}

console.log([...map(LLNode.mergeSort(l) || [], (v) => v.value)]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment