mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 17:29:52 +08:00
add zig codes for Section 'Binary Search' (binary_search.zig), 'Hash Search' (hashing_search.zig)
This commit is contained in:
@@ -33,4 +33,18 @@ pub fn listToLinkedList(comptime T: type, mem_allocator: std.mem.Allocator, list
|
||||
head = head.next.?;
|
||||
}
|
||||
return dum.next;
|
||||
}
|
||||
|
||||
// Generate a linked list with an array
|
||||
pub fn arrToLinkedList(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*ListNode(T) {
|
||||
var dum = try mem_allocator.create(ListNode(T));
|
||||
dum.init(0);
|
||||
var head = dum;
|
||||
for (arr) |val| {
|
||||
var tmp = try mem_allocator.create(ListNode(T));
|
||||
tmp.init(val);
|
||||
head.next = tmp;
|
||||
head = head.next.?;
|
||||
}
|
||||
return dum.next;
|
||||
}
|
||||
Reference in New Issue
Block a user