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.
dotnet add package LeetCodeUtils
NuGet: https://www.nuget.org/packages/LeetCodeUtils
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
git clone https://github.com/wblackmon/LeetCodeUtils.git
cd LeetCodeUtils
dotnet builddotnet run --project src/LeetCodeUtils.Consoledotnet testusing 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 -> 4using 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));public class ListNode {
public int val;
public ListNode next;
}public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
}FromArray(int[])ToArray(ListNode)ToString(ListNode)Append(ListNode, int)Reverse(ListNode)
FromLevelOrder(int?[])ToLevelOrder(TreeNode)ToPrettyString(TreeNode)Height(TreeNode)IsBalanced(TreeNode)
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 testBuild and pack:
dotnet pack src/LeetCodeUtils/LeetCodeUtils.csproj -c Release -o ./artifactsPublish (Trusted Publishing via GitHub Actions):
dotnet nuget push ./artifacts/*.nupkg \
--source https://api.nuget.org/v3/index.json \
--skip-duplicate- PrettyPrint ASCII renderer for binary trees
- Random linked list & tree generators
- Additional conversion helpers
- Expanded test coverage
MIT License Β© Wayne Eliot Blackmon