leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 8459f619fde2d607bdefdde05d9c61c7ca072391
parent f6adfd3d0d32487e8e45eaba341b80704227b3a9
Author: Jack Sangdahl <jack@pngu.org>
Date:   Mon,  9 Feb 2026 15:52:08 -0700

chore: Add Python ListNode, TreeNode helpers

Diffstat:
Mleetcode.py | 27+++++++++++++++++++++++++++
Mreadme.md | 34+++++++++++++++++++++++++++++++---
2 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/leetcode.py b/leetcode.py @@ -26,6 +26,23 @@ def treenode_build(vals): return root +def treenode_print(root): + if not root: + return + q, res = deque([root]), [] + while q: + curr = q.popleft() + if curr: + res.append(str(curr.val)) + q.append(curr.left) + q.append(curr.right) + else: + res.append("null") + while res and res[-1] == "null": + res.pop() + print(res) + + class ListNode: def __init__(self, val=0, next=None): self.val = val @@ -41,3 +58,13 @@ def listnode_build(vals): curr.next = ListNode(i) curr = curr.next return head + + +def listnode_print(head): + if not head: + return + res, curr = [], head + while curr: + res.append(str(curr.val)) + curr = curr.next + print(res) diff --git a/readme.md b/readme.md @@ -1,10 +1,11 @@ -## Leetcode +# Leetcode This repository contains my Leetcode solutions for [user 0x6A73](https://leetcode.com/u/0x6A73). Generally, the daily challenge is solved in both C and Python3. -### Notes +## Notes -The [`leetcode.h`](leetcode.h) header file contains various includes, standard Leetcode structs (`ListNode`, `TreeNode`), and some helpful macros, like calculating the size of a C array using `builtin` functions. +### C Utilities +The [`leetcode.h`](leetcode.h) header file contains various includes, standard Leetcode structs (`ListNode`, `TreeNode`), and some helpful macros, like calculating the size of a array using `builtin` functions. ```c #define IS_ARRAY(value) \ (!__builtin_types_compatible_p(typeof((value)), typeof(&(value)[0]))) @@ -29,6 +30,7 @@ int main() { int input[] = {0, 3, 1, 0, 4, 5, 2, 0}; struct ListNode *head = listnode_build(input, ARRAY_SIZE(input)); listnode_print(head); + listnode_free(head); } ``` @@ -43,6 +45,7 @@ int main() { // this value, use treenode_create() to individually create each node struct TreeNode *root = treenode_build(input, ARRAY_SIZE(input)); treenode_print(root); + treenode_free(root); } ``` @@ -59,3 +62,28 @@ int main() { two_d_arr_free(edges); } ``` + +### Python Utilities + +Similar to the above, [`leetcode.py`](leetcode.py) contains helpers to work with Leetcode data structures. + +#### `ListNode` data type + +```python +from leetcode import ListNode, listnode_build, listnode_print + +if __name__ == "__main__": + head = listnode_build(vals=[0, 3, 1, 0, 4, 5, 2, 0]) + listnode_print(head) +``` + +#### `TreeNode` data type + +```python +from leetcode import TreeNode, treenode_build, treenode_print + +if __name__ == "__main__": + root = treenode_build(vals=[3, 9, 20, None, None, 15, 7]) + # note: None denotes a 'null' leaf + treenode_print(root) +```