npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@omts/list-node

v0.0.1

Published

A versatile utility library for creating and manipulating singly linked lists (`ListNode`) in TypeScript. This package supports generic types, allowing you to build linked lists for any data type, and provides various operations such as insertion, deletio

Downloads

3

Readme

@omts/list-node 🚀

A versatile utility library for creating and manipulating singly linked lists (ListNode) in TypeScript. This package supports generic types, allowing you to build linked lists for any data type, and provides various operations such as insertion, deletion, reversing, and more.

Installation 📦

Install the package via npm or pnpm:

npm install @omts/list-node

or

pnpm add @omts/list-node

Usage ✨

This package exports a ListNode class with a variety of static methods for working with singly linked lists. It supports generic types, making it flexible for use with different data types.

Example: Creating a Linked List

import { ListNode } from '@omts/list-node';

// Create a linked list from an array of numbers
const list = ListNode.createFromArray([1, 2, 3, 4]);
console.log(ListNode.toArray(list)); // Output: [1, 2, 3, 4]

// Get the length of the linked list
const length = ListNode.size(list);
console.log(length); // Output: 4

Example: Inserting, Deleting, and Reversing

// Insert a value at index 2
const newList = ListNode.insertAt(list, 2, 99);
console.log(ListNode.toArray(newList)); // Output: [1, 2, 99, 3, 4]

// Delete the value at index 1
const afterDeletion = ListNode.deleteAt(newList, 1);
console.log(ListNode.toArray(afterDeletion)); // Output: [1, 99, 3, 4]

// Reverse the linked list
const reversedList = ListNode.reverse(afterDeletion);
console.log(ListNode.toArray(reversedList)); // Output: [4, 3, 99, 1]

Example: Detecting Cycles and Merging Lists

// Check if a linked list has a cycle
const hasCycle = ListNode.hasCycle(list);
console.log(hasCycle); // Output: false

// Merge two sorted linked lists
const list1 = ListNode.createFromArray([1, 3, 5]);
const list2 = ListNode.createFromArray([2, 4, 6]);
const mergedList = ListNode.mergeTwoLists(list1, list2);
console.log(ListNode.toArray(mergedList)); // Output: [1, 2, 3, 4, 5, 6]

API Documentation 📚

  • ListNode.createFromArray(arr: T[]): ListNode<T> | null: Creates a linked list from an array of values.
  • ListNode.toArray(head: ListNode<T> | null): T[]: Converts a linked list back to an array.
  • ListNode.size(head: ListNode<T> | null): number: Returns the number of nodes in the linked list.
  • ListNode.insertAt(head: ListNode<T> | null, index: number, val: T): ListNode<T> | null: Inserts a new node at a specific position in the list.
  • ListNode.deleteAt(head: ListNode<T> | null, index: number): ListNode<T> | null: Deletes a node from a specific position in the list.
  • ListNode.find(head: ListNode<T> | null, val: T): ListNode<T> | null: Finds a node by its value and returns it.
  • ListNode.reverse(head: ListNode<T> | null): ListNode<T> | null: Reverses the linked list.
  • ListNode.append(head: ListNode<T> | null, val: T): ListNode<T> | null: Appends a new node to the end of the linked list.
  • ListNode.prepend(head: ListNode<T> | null, val: T): ListNode<T>: Prepends a new node to the beginning of the linked list.
  • ListNode.clear(head: ListNode<T> | null): ListNode<T> | null: Clears the linked list.
  • ListNode.hasCycle(head: ListNode<T> | null): boolean: Detects if the linked list contains a cycle.
  • ListNode.mergeTwoLists(l1: ListNode<T> | null, l2: ListNode<T> | null): ListNode<T> | null: Merges two sorted linked lists into one.

Key Features ✨

  • Generic Support: The linked list is implemented with TypeScript generics, allowing you to use it with any data type.
  • Comprehensive Linked List Operations: Provides essential linked list operations such as insertion, deletion, reversal, and more.
  • Cycle Detection: Detect if a linked list contains a cycle.
  • Merge Sorted Lists: Easily merge two sorted linked lists into one.

Development 🛠️

Install dependencies:

pnpm install

Run tests:

pnpm run test

Build the project:

pnpm run build

License ⚖️

This project is licensed under the MIT License - see the LICENSE file for details.