Skip to content

wblackmon/LeetCodeUtils

Repository files navigation

LeetCodeUtils

LeetCodeUtils is a lightweight .NET 8 utility library providing canonical data structures and helper methods commonly used in LeetCode problems.
It includes:

  • ListNode (singly linked list)
  • TreeNode (binary tree)
  • ListNodeUtils (creation, conversion, printing)
  • TreeNodeUtils (creation, conversion, pretty‑printing)

Designed for interview prep, algorithm practice, and cleaner LeetCode solutions.


πŸ“¦ Install via NuGet

dotnet add package LeetCodeUtils

NuGet: https://www.nuget.org/packages/LeetCodeUtils


πŸ“‚ Solution Structure

LeetCodeUtils.sln
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ LeetCodeUtils/              # Class Library (NuGet package)
β”‚   β”‚   β”œβ”€β”€ ListNode.cs
β”‚   β”‚   β”œβ”€β”€ TreeNode.cs
β”‚   β”‚   β”œβ”€β”€ ListNodeUtils.cs
β”‚   β”‚   └── TreeNodeUtils.cs
β”‚   β”‚
β”‚   └── LeetCodeUtils.Console/      # Console demo
β”‚       └── Program.cs
β”‚
└── tests/
    └── LeetCodeUtils.Tests/        # xUnit test suite
        β”œβ”€β”€ ListNodeUtilsTests.cs
        └── TreeNodeUtilsTests.cs

πŸš€ Getting Started

Build the solution

git clone https://github.com/wblackmon/LeetCodeUtils.git
cd LeetCodeUtils
dotnet build

Run the console demo

dotnet run --project src/LeetCodeUtils.Console

Run tests

dotnet test

πŸ”— Usage Examples

Linked List Example

using LeetCodeUtils;

// Create a linked list from an array
var head = ListNodeUtils.FromArray(new[] { 1, 2, 3, 4 });

// Convert back to array
int[] values = ListNodeUtils.ToArray(head);

// Pretty-print the list
Console.WriteLine(ListNodeUtils.ToString(head));
// Output: 1 -> 2 -> 3 -> 4

Binary Tree Example

using LeetCodeUtils;

// Create a binary tree from level-order input
var root = TreeNodeUtils.FromLevelOrder(new int?[] { 1, 2, 3, null, 5 });

// Pretty-print the tree
Console.WriteLine(TreeNodeUtils.ToPrettyString(root));

🧱 Included Data Structures

ListNode

public class ListNode {
    public int val;
    public ListNode next;
}

TreeNode

public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;
}

πŸ”§ Utility Methods

ListNodeUtils

  • FromArray(int[])
  • ToArray(ListNode)
  • ToString(ListNode)
  • Append(ListNode, int)
  • Reverse(ListNode)

TreeNodeUtils

  • FromLevelOrder(int?[])
  • ToLevelOrder(TreeNode)
  • ToPrettyString(TreeNode)
  • Height(TreeNode)
  • IsBalanced(TreeNode)

πŸ§ͺ Testing

Example xUnit test:

[Fact]
public void ToString_ShouldReturnCorrectFormat()
{
    var head = ListNodeUtils.FromArray(new[] { 1, 2, 3 });
    Assert.Equal("1 -> 2 -> 3", ListNodeUtils.ToString(head));
}

Run tests:

dotnet test

πŸ“¦ NuGet Packaging

Build and pack:

dotnet pack src/LeetCodeUtils/LeetCodeUtils.csproj -c Release -o ./artifacts

Publish (Trusted Publishing via GitHub Actions):

dotnet nuget push ./artifacts/*.nupkg \
  --source https://api.nuget.org/v3/index.json \
  --skip-duplicate

πŸ—οΈ Roadmap

  • PrettyPrint ASCII renderer for binary trees
  • Random linked list & tree generators
  • Additional conversion helpers
  • Expanded test coverage

πŸ“œ License

MIT License Β© Wayne Eliot Blackmon

About

LeetCodeUtils is a lightweight .NET 8 library providing canonical data structures and helper utilities for solving algorithm problems. Includes ListNode and TreeNode with creation and printing methods, plus console demos and xUnit tests. Ideal for LeetCode practice and interview prep.

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Packages

 
 
 

Contributors