aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-08-25 13:06:21 -0600
committerJack Sangdahl <[email protected]>2025-08-25 13:06:21 -0600
commit3f521a9849997608712e92ad8e90e83ff1cefdc0 (patch)
tree7b8224a755318088a78d30c7bb2ddfa9e2473bae
parent07c9e6547189b072d990fb861194449cdc14f5c5 (diff)
downloadleetcode-3f521a9849997608712e92ad8e90e83ff1cefdc0.tar.gz
leetcode-3f521a9849997608712e92ad8e90e83ff1cefdc0.zip
chore: Tidy util files and add listnode_build
Removes unnecessary C++ specific header file code Adds C and Python helper functions to build a full ListNode based on an integer array as input
-rw-r--r--leetcode.c11
-rw-r--r--leetcode.h40
-rw-r--r--leetcode.py19
3 files changed, 31 insertions, 39 deletions
diff --git a/leetcode.c b/leetcode.c
index b6fe769..b7c3b22 100644
--- a/leetcode.c
+++ b/leetcode.c
@@ -27,6 +27,17 @@ struct ListNode *listnode_create(int val) {
return new;
}
+struct ListNode *listnode_build(int *vals, int size) {
+ if (!size)
+ return NULL;
+ struct ListNode *head = listnode_create(vals[0]), *curr = head;
+ for (int i = 1; i < size; i++) {
+ curr->next = listnode_create(vals[i]);
+ curr = curr->next;
+ }
+ return head;
+}
+
void listnode_print(struct ListNode *head) {
struct ListNode *curr = head;
while (curr != NULL) {
diff --git a/leetcode.h b/leetcode.h
index 0f0ca7c..cc17fc2 100644
--- a/leetcode.h
+++ b/leetcode.h
@@ -1,4 +1,5 @@
#pragma once
+
#include "lib/uthash/src/uthash.h"
#include <assert.h>
#include <ctype.h>
@@ -11,43 +12,6 @@
#include <stdlib.h>
#include <string.h>
-#ifdef __cplusplus
-#include <bits/stdc++.h>
-using namespace std;
-
-typedef vector<vector<int>> vvd;
-
-struct ListNode {
- int val;
- ListNode *next;
- ListNode() : val(0), next(nullptr) {}
- ListNode(int x) : val(x), next(nullptr) {}
- ListNode(int x, ListNode *next) : val(x), next(next) {}
-};
-
-struct TreeNode {
- int val;
- TreeNode *left;
- TreeNode *right;
- TreeNode() : val(0), left(nullptr), right(nullptr) {}
- TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- TreeNode(int x, TreeNode *left, TreeNode *right)
- : val(x), left(left), right(right) {}
-};
-
-class Node {
-public:
- int val;
- vector<Node *> children;
- Node() {}
- Node(int _val) { val = _val; }
- Node(int _val, vector<Node *> _children) {
- val = _val;
- children = _children;
- }
-};
-#else //__cplusplus
-
struct two_d_arr {
int row_size;
int *col_size;
@@ -76,6 +40,7 @@ void two_d_arr_init(struct two_d_arr *obj, int row_size, int col_size,
void two_d_arr_free(struct two_d_arr *obj);
struct ListNode *listnode_create(int val);
+struct ListNode *listnode_build(int *vals, int size);
void listnode_print(struct ListNode *head);
void listnode_free(struct ListNode *head);
@@ -94,4 +59,3 @@ void node_free(struct Node *root);
#define ARRAY_SIZE(array) \
(__builtin_choose_expr(IS_ARRAY((array)), \
sizeof((array)) / sizeof((array)[0]), (void)0))
-#endif
diff --git a/leetcode.py b/leetcode.py
index 6fdbe28..29f7144 100644
--- a/leetcode.py
+++ b/leetcode.py
@@ -9,7 +9,7 @@ class TreeNode:
def treenode_build(vals):
- if not vals or vals[0] is None:
+ if not vals:
return None
root = TreeNode(vals[0])
q, i = deque([root]), 1
@@ -24,3 +24,20 @@ def treenode_build(vals):
q.append(curr.right)
i += 1
return root
+
+
+class ListNode:
+ def __init__(self, val=0, next=None):
+ self.val = val
+ self.next = next
+
+
+def listnode_build(vals):
+ if not vals or vals[0] is None:
+ return None
+ head = ListNode(vals[0])
+ curr = head
+ for i in vals[1:]:
+ curr.next = ListNode(i)
+ curr = curr.next
+ return head